Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini less implementation for busybox |
| 4 | * |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005 by Rob Sullivan <cogito.ergo.cogito@gmail.com> |
| 6 | * |
Mike Frysinger | f284c76 | 2006-04-16 20:38:26 +0000 | [diff] [blame] | 7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
| 8 | */ |
| 9 | |
| 10 | /* |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 11 | * TODO: |
| 12 | * - Add more regular expression support - search modifiers, certain matches, etc. |
| 13 | * - Add more complex bracket searching - currently, nested brackets are |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 14 | * not considered. |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 15 | * - Add support for "F" as an input. This causes less to act in |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 16 | * a similar way to tail -f. |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 17 | * - Allow horizontal scrolling. |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 18 | * |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 19 | * Notes: |
| 20 | * - the inp file pointer is used so that keyboard input works after |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 21 | * redirected input has been read from stdin |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 22 | */ |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | 18d6fc1 | 2007-03-08 17:52:36 +0000 | [diff] [blame] | 24 | #include <sched.h> /* sched_yield() */ |
| 25 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 26 | #include "libbb.h" |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 27 | #if ENABLE_FEATURE_LESS_REGEXP |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 28 | #include "xregex.h" |
| 29 | #endif |
| 30 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 31 | /* FIXME: currently doesn't work right */ |
| 32 | #undef ENABLE_FEATURE_LESS_FLAGCS |
| 33 | #define ENABLE_FEATURE_LESS_FLAGCS 0 |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 34 | |
| 35 | /* The escape codes for highlighted and normal text */ |
| 36 | #define HIGHLIGHT "\033[7m" |
| 37 | #define NORMAL "\033[0m" |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 38 | /* The escape code to clear the screen */ |
"Vladimir N. Oleynik" | a0ae6de | 2005-09-19 10:28:43 +0000 | [diff] [blame] | 39 | #define CLEAR "\033[H\033[J" |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 40 | /* The escape code to clear to end of line */ |
| 41 | #define CLEAR_2_EOL "\033[K" |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 43 | /* These are the escape sequences corresponding to special keys */ |
| 44 | enum { |
| 45 | REAL_KEY_UP = 'A', |
| 46 | REAL_KEY_DOWN = 'B', |
| 47 | REAL_KEY_RIGHT = 'C', |
| 48 | REAL_KEY_LEFT = 'D', |
| 49 | REAL_PAGE_UP = '5', |
| 50 | REAL_PAGE_DOWN = '6', |
Denis Vlasenko | f5a1576 | 2007-03-09 08:55:23 +0000 | [diff] [blame] | 51 | REAL_KEY_HOME = '7', // vt100? linux vt? or what? |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 52 | REAL_KEY_END = '8', |
Denis Vlasenko | f5a1576 | 2007-03-09 08:55:23 +0000 | [diff] [blame] | 53 | REAL_KEY_HOME_ALT = '1', // ESC [1~ (vt100? linux vt? or what?) |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 54 | REAL_KEY_END_ALT = '4', // ESC [4~ |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 55 | REAL_KEY_HOME_XTERM = 'H', |
| 56 | REAL_KEY_END_XTERM = 'F', |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 57 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 58 | /* These are the special codes assigned by this program to the special keys */ |
| 59 | KEY_UP = 20, |
| 60 | KEY_DOWN = 21, |
| 61 | KEY_RIGHT = 22, |
| 62 | KEY_LEFT = 23, |
| 63 | PAGE_UP = 24, |
| 64 | PAGE_DOWN = 25, |
| 65 | KEY_HOME = 26, |
| 66 | KEY_END = 27, |
| 67 | |
| 68 | /* Absolute max of lines eaten */ |
| 69 | MAXLINES = CONFIG_FEATURE_LESS_MAXLINES, |
| 70 | |
| 71 | /* This many "after the end" lines we will show (at max) */ |
| 72 | TILDES = 1, |
| 73 | }; |
| 74 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 75 | /* Command line options */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 76 | enum { |
| 77 | FLAG_E = 1, |
| 78 | FLAG_M = 1 << 1, |
| 79 | FLAG_m = 1 << 2, |
| 80 | FLAG_N = 1 << 3, |
| 81 | FLAG_TILDE = 1 << 4, |
Bernhard Reutner-Fischer | 882e60c | 2006-12-04 16:04:50 +0000 | [diff] [blame] | 82 | /* hijack command line options variable for internal state vars */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 83 | LESS_STATE_MATCH_BACKWARDS = 1 << 15, |
| 84 | }; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 85 | |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 86 | #if !ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 87 | enum { pattern_valid = 0 }; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 88 | #endif |
| 89 | |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 90 | struct globals { |
| 91 | int cur_fline; /* signed */ |
| 92 | int kbd_fd; /* fd to get input from */ |
| 93 | /* last position in last line, taking into account tabs */ |
| 94 | size_t linepos; |
| 95 | unsigned max_displayed_line; |
| 96 | unsigned max_fline; |
| 97 | unsigned max_lineno; /* this one tracks linewrap */ |
| 98 | unsigned width; |
| 99 | ssize_t eof_error; /* eof if 0, error if < 0 */ |
| 100 | size_t readpos; |
| 101 | size_t readeof; |
| 102 | const char **buffer; |
| 103 | const char **flines; |
| 104 | const char *empty_line_marker; |
| 105 | unsigned num_files; |
| 106 | unsigned current_file; |
| 107 | char *filename; |
| 108 | char **files; |
| 109 | #if ENABLE_FEATURE_LESS_MARKS |
| 110 | unsigned num_marks; |
| 111 | unsigned mark_lines[15][2]; |
| 112 | #endif |
| 113 | #if ENABLE_FEATURE_LESS_REGEXP |
| 114 | unsigned *match_lines; |
| 115 | int match_pos; /* signed! */ |
| 116 | unsigned num_matches; |
| 117 | regex_t pattern; |
| 118 | smallint pattern_valid; |
| 119 | #endif |
| 120 | smallint terminated; |
| 121 | struct termios term_orig, term_less; |
| 122 | }; |
| 123 | #define G (*ptr_to_globals) |
| 124 | #define cur_fline (G.cur_fline ) |
| 125 | #define kbd_fd (G.kbd_fd ) |
| 126 | #define linepos (G.linepos ) |
| 127 | #define max_displayed_line (G.max_displayed_line) |
| 128 | #define max_fline (G.max_fline ) |
| 129 | #define max_lineno (G.max_lineno ) |
| 130 | #define width (G.width ) |
| 131 | #define eof_error (G.eof_error ) |
| 132 | #define readpos (G.readpos ) |
| 133 | #define readeof (G.readeof ) |
| 134 | #define buffer (G.buffer ) |
| 135 | #define flines (G.flines ) |
| 136 | #define empty_line_marker (G.empty_line_marker ) |
| 137 | #define num_files (G.num_files ) |
| 138 | #define current_file (G.current_file ) |
| 139 | #define filename (G.filename ) |
| 140 | #define files (G.files ) |
| 141 | #define num_marks (G.num_marks ) |
| 142 | #define mark_lines (G.mark_lines ) |
Denis Vlasenko | 342b0ab | 2007-05-31 23:06:18 +0000 | [diff] [blame] | 143 | #if ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 144 | #define match_lines (G.match_lines ) |
| 145 | #define match_pos (G.match_pos ) |
| 146 | #define num_matches (G.num_matches ) |
| 147 | #define pattern (G.pattern ) |
| 148 | #define pattern_valid (G.pattern_valid ) |
Denis Vlasenko | 342b0ab | 2007-05-31 23:06:18 +0000 | [diff] [blame] | 149 | #endif |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 150 | #define terminated (G.terminated ) |
| 151 | #define term_orig (G.term_orig ) |
| 152 | #define term_less (G.term_less ) |
| 153 | #define INIT_G() do { \ |
| 154 | PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ |
| 155 | empty_line_marker = "~"; \ |
| 156 | num_files = 1; \ |
| 157 | current_file = 1; \ |
| 158 | eof_error = 1; \ |
| 159 | terminated = 1; \ |
| 160 | } while (0) |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 161 | |
| 162 | /* Reset terminal input to normal */ |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 163 | static void set_tty_cooked(void) |
| 164 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 165 | fflush(stdout); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 166 | tcsetattr(kbd_fd, TCSANOW, &term_orig); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 169 | /* Exit the program gracefully */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 170 | static void less_exit(int code) |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 171 | { |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 172 | /* TODO: We really should save the terminal state when we start, |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 173 | * and restore it when we exit. Less does this with the |
| 174 | * "ti" and "te" termcap commands; can this be done with |
| 175 | * only termios.h? */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 176 | bb_putchar('\n'); |
Bernhard Reutner-Fischer | 882e60c | 2006-12-04 16:04:50 +0000 | [diff] [blame] | 177 | fflush_stdout_and_exit(code); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 178 | } |
| 179 | |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 180 | /* 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] | 181 | top-left corner of the console */ |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 182 | static void move_cursor(int line, int row) |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 183 | { |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 184 | printf("\033[%u;%uH", line, row); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 187 | static void clear_line(void) |
| 188 | { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 189 | printf("\033[%u;0H" CLEAR_2_EOL, max_displayed_line + 2); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void print_hilite(const char *str) |
| 193 | { |
| 194 | printf(HIGHLIGHT"%s"NORMAL, str); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 197 | static void print_statusline(const char *str) |
| 198 | { |
| 199 | clear_line(); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 200 | printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str); |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 203 | #if ENABLE_FEATURE_LESS_REGEXP |
| 204 | static void fill_match_lines(unsigned pos); |
| 205 | #else |
| 206 | #define fill_match_lines(pos) ((void)0) |
| 207 | #endif |
| 208 | |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 209 | /* Devilishly complex routine. |
| 210 | * |
| 211 | * Has to deal with EOF and EPIPE on input, |
| 212 | * with line wrapping, with last line not ending in '\n' |
| 213 | * (possibly not ending YET!), with backspace and tabs. |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 214 | * It reads input again if last time we got an EOF (thus supporting |
| 215 | * growing files) or EPIPE (watching output of slow process like make). |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 216 | * |
| 217 | * Variables used: |
| 218 | * flines[] - array of lines already read. Linewrap may cause |
| 219 | * one source file line to occupy several flines[n]. |
| 220 | * flines[max_fline] - last line, possibly incomplete. |
| 221 | * terminated - 1 if flines[max_fline] is 'terminated' |
| 222 | * (if there was '\n' [which isn't stored itself, we just remember |
| 223 | * that it was seen]) |
| 224 | * max_lineno - last line's number, this one doesn't increment |
| 225 | * on line wrap, only on "real" new lines. |
| 226 | * readbuf[0..readeof-1] - small preliminary buffer. |
| 227 | * readbuf[readpos] - next character to add to current line. |
| 228 | * linepos - screen line position of next char to be read |
| 229 | * (takes into account tabs and backspaces) |
| 230 | * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error |
| 231 | */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 232 | static void read_lines(void) |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 233 | { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 234 | #define readbuf bb_common_bufsiz1 |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 235 | char *current_line, *p; |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 236 | USE_FEATURE_LESS_REGEXP(unsigned old_max_fline = max_fline;) |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 237 | int w = width; |
| 238 | char last_terminated = terminated; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 239 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 240 | if (option_mask32 & FLAG_N) |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 241 | w -= 8; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 242 | |
| 243 | current_line = xmalloc(w); |
| 244 | p = current_line; |
| 245 | max_fline += last_terminated; |
| 246 | if (!last_terminated) { |
| 247 | const char *cp = flines[max_fline]; |
| 248 | if (option_mask32 & FLAG_N) |
| 249 | cp += 8; |
| 250 | strcpy(current_line, cp); |
| 251 | p += strlen(current_line); |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 252 | /* linepos is still valid from previous read_lines() */ |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 253 | } else { |
| 254 | linepos = 0; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 255 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 256 | |
| 257 | while (1) { |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 258 | again: |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 259 | *p = '\0'; |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 260 | terminated = 0; |
| 261 | while (1) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 262 | char c; |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 263 | /* if no unprocessed chars left, eat more */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 264 | if (readpos >= readeof) { |
Denis Vlasenko | 18d6fc1 | 2007-03-08 17:52:36 +0000 | [diff] [blame] | 265 | smallint yielded = 0; |
| 266 | |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 267 | ndelay_on(0); |
Denis Vlasenko | 18d6fc1 | 2007-03-08 17:52:36 +0000 | [diff] [blame] | 268 | read_again: |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 269 | eof_error = safe_read(0, readbuf, sizeof(readbuf)); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 270 | readpos = 0; |
| 271 | readeof = eof_error; |
| 272 | if (eof_error < 0) { |
Denis Vlasenko | 18d6fc1 | 2007-03-08 17:52:36 +0000 | [diff] [blame] | 273 | if (errno == EAGAIN && !yielded) { |
| 274 | /* We can hit EAGAIN while searching for regexp match. |
| 275 | * Yield is not 100% reliable solution in general, |
Denis Vlasenko | f5a1576 | 2007-03-09 08:55:23 +0000 | [diff] [blame] | 276 | * but for less it should be good enough - |
| 277 | * we give stdin supplier some CPU time to produce |
| 278 | * more input. We do it just once. |
| 279 | * Currently, we do not stop when we found the Nth |
| 280 | * occurrence we were looking for. We read till end |
| 281 | * (or double EAGAIN). TODO? */ |
Denis Vlasenko | 18d6fc1 | 2007-03-08 17:52:36 +0000 | [diff] [blame] | 282 | sched_yield(); |
| 283 | yielded = 1; |
| 284 | goto read_again; |
| 285 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 286 | readeof = 0; |
| 287 | if (errno != EAGAIN) |
| 288 | print_statusline("read error"); |
| 289 | } |
Denis Vlasenko | 18d6fc1 | 2007-03-08 17:52:36 +0000 | [diff] [blame] | 290 | ndelay_off(0); |
| 291 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 292 | if (eof_error <= 0) { |
| 293 | goto reached_eof; |
| 294 | } |
| 295 | } |
| 296 | c = readbuf[readpos]; |
Denis Vlasenko | 50ddabc | 2006-12-31 19:36:01 +0000 | [diff] [blame] | 297 | /* backspace? [needed for manpages] */ |
| 298 | /* <tab><bs> is (a) insane and */ |
| 299 | /* (b) harder to do correctly, so we refuse to do it */ |
| 300 | if (c == '\x8' && linepos && p[-1] != '\t') { |
Denis Vlasenko | 95b3071 | 2006-12-31 19:23:31 +0000 | [diff] [blame] | 301 | readpos++; /* eat it */ |
Denis Vlasenko | 95b3071 | 2006-12-31 19:23:31 +0000 | [diff] [blame] | 302 | linepos--; |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 303 | /* was buggy (p could end up <= current_line)... */ |
Denis Vlasenko | 50ddabc | 2006-12-31 19:36:01 +0000 | [diff] [blame] | 304 | *--p = '\0'; |
Denis Vlasenko | 95b3071 | 2006-12-31 19:23:31 +0000 | [diff] [blame] | 305 | continue; |
| 306 | } |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 307 | { |
| 308 | size_t new_linepos = linepos + 1; |
| 309 | if (c == '\t') { |
| 310 | new_linepos += 7; |
| 311 | new_linepos &= (~7); |
| 312 | } |
| 313 | if (new_linepos >= w) |
| 314 | break; |
| 315 | linepos = new_linepos; |
| 316 | } |
Denis Vlasenko | 95b3071 | 2006-12-31 19:23:31 +0000 | [diff] [blame] | 317 | /* ok, we will eat this char */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 318 | readpos++; |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 319 | if (c == '\n') { |
| 320 | terminated = 1; |
| 321 | linepos = 0; |
| 322 | break; |
| 323 | } |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 324 | /* NUL is substituted by '\n'! */ |
| 325 | if (c == '\0') c = '\n'; |
| 326 | *p++ = c; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 327 | *p = '\0'; |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 328 | } |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 329 | /* Corner case: linewrap with only "" wrapping to next line */ |
| 330 | /* Looks ugly on screen, so we do not store this empty line */ |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 331 | if (!last_terminated && !current_line[0]) { |
| 332 | last_terminated = 1; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 333 | max_lineno++; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 334 | goto again; |
| 335 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 336 | reached_eof: |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 337 | last_terminated = terminated; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 338 | flines = xrealloc(flines, (max_fline+1) * sizeof(char *)); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 339 | if (option_mask32 & FLAG_N) { |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 340 | /* Width of 7 preserves tab spacing in the text */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 341 | flines[max_fline] = xasprintf( |
| 342 | (max_lineno <= 9999999) ? "%7u %s" : "%07u %s", |
| 343 | max_lineno % 10000000, current_line); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 344 | free(current_line); |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 345 | if (terminated) |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 346 | max_lineno++; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 347 | } else { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 348 | flines[max_fline] = xrealloc(current_line, strlen(current_line)+1); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 349 | } |
Denis Vlasenko | fdcfa2a | 2007-05-31 23:55:39 +0000 | [diff] [blame] | 350 | if (max_fline >= MAXLINES) { |
| 351 | eof_error = 0; /* Pretend we saw EOF */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 352 | break; |
Denis Vlasenko | fdcfa2a | 2007-05-31 23:55:39 +0000 | [diff] [blame] | 353 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 354 | if (max_fline > cur_fline + max_displayed_line) |
| 355 | break; |
| 356 | if (eof_error <= 0) { |
| 357 | if (eof_error < 0 && errno == EAGAIN) { |
| 358 | /* not yet eof or error, reset flag (or else |
| 359 | * we will hog CPU - select() will return |
| 360 | * immediately */ |
| 361 | eof_error = 1; |
| 362 | } |
| 363 | break; |
| 364 | } |
| 365 | max_fline++; |
| 366 | current_line = xmalloc(w); |
| 367 | p = current_line; |
| 368 | linepos = 0; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 369 | } |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 370 | fill_match_lines(old_max_fline); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 371 | #undef readbuf |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 374 | #if ENABLE_FEATURE_LESS_FLAGS |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 375 | /* Interestingly, writing calc_percent as a function saves around 32 bytes |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 376 | * on my build. */ |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 377 | static int calc_percent(void) |
| 378 | { |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 379 | 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] | 380 | return p <= 100 ? p : 100; |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 383 | /* Print a status line if -M was specified */ |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 384 | static void m_status_print(void) |
| 385 | { |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 386 | int percentage; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 387 | |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 388 | clear_line(); |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 389 | printf(HIGHLIGHT"%s", filename); |
| 390 | if (num_files > 1) |
| 391 | printf(" (file %i of %i)", current_file, num_files); |
| 392 | printf(" lines %i-%i/%i ", |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 393 | cur_fline + 1, cur_fline + max_displayed_line + 1, |
| 394 | max_fline + 1); |
| 395 | if (cur_fline >= max_fline - max_displayed_line) { |
Denis Vlasenko | d51d14e | 2006-12-21 13:57:37 +0000 | [diff] [blame] | 396 | printf("(END)"NORMAL); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 397 | if (num_files > 1 && current_file != num_files) |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 398 | printf(HIGHLIGHT" - next: %s"NORMAL, files[current_file]); |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 399 | return; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 400 | } |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 401 | percentage = calc_percent(); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 402 | printf("%i%%"NORMAL, percentage); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 403 | } |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 404 | #endif |
| 405 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 406 | /* Print the status line */ |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 407 | static void status_print(void) |
| 408 | { |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 409 | const char *p; |
| 410 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 411 | /* Change the status if flags have been set */ |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 412 | #if ENABLE_FEATURE_LESS_FLAGS |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 413 | if (option_mask32 & (FLAG_M|FLAG_m)) { |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 414 | m_status_print(); |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 415 | return; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 416 | } |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 417 | /* No flags set */ |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 418 | #endif |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 419 | |
| 420 | clear_line(); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 421 | if (cur_fline && cur_fline < max_fline - max_displayed_line) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 422 | bb_putchar(':'); |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 423 | return; |
| 424 | } |
Denis Vlasenko | d51d14e | 2006-12-21 13:57:37 +0000 | [diff] [blame] | 425 | p = "(END)"; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 426 | if (!cur_fline) |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 427 | p = filename; |
| 428 | if (num_files > 1) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 429 | printf(HIGHLIGHT"%s (file %i of %i)"NORMAL, |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 430 | p, current_file, num_files); |
| 431 | return; |
| 432 | } |
| 433 | print_hilite(p); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Denis Vlasenko | 5c1de36 | 2007-03-08 16:44:32 +0000 | [diff] [blame] | 436 | static void cap_cur_fline(int nlines) |
| 437 | { |
| 438 | int diff; |
| 439 | if (cur_fline < 0) |
| 440 | cur_fline = 0; |
| 441 | if (cur_fline + max_displayed_line > max_fline + TILDES) { |
| 442 | cur_fline -= nlines; |
| 443 | if (cur_fline < 0) |
| 444 | cur_fline = 0; |
| 445 | diff = max_fline - (cur_fline + max_displayed_line) + TILDES; |
| 446 | /* As the number of lines requested was too large, we just move |
| 447 | to the end of the file */ |
| 448 | if (diff > 0) |
| 449 | cur_fline += diff; |
| 450 | } |
| 451 | } |
| 452 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 453 | static const char controls[] ALIGN1 = |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 454 | /* NUL: never encountered; TAB: not converted */ |
| 455 | /**/"\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] | 456 | "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" |
| 457 | "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 458 | static const char ctrlconv[] ALIGN1 = |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 459 | /* '\n': it's a former NUL - subst with '@', not 'J' */ |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 460 | "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f" |
| 461 | "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"; |
| 462 | |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 463 | #if ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 464 | static void print_found(const char *line) |
| 465 | { |
| 466 | int match_status; |
| 467 | int eflags; |
| 468 | char *growline; |
| 469 | regmatch_t match_structs; |
| 470 | |
| 471 | char buf[width]; |
| 472 | const char *str = line; |
| 473 | char *p = buf; |
| 474 | size_t n; |
| 475 | |
| 476 | while (*str) { |
| 477 | n = strcspn(str, controls); |
| 478 | if (n) { |
| 479 | if (!str[n]) break; |
| 480 | memcpy(p, str, n); |
| 481 | p += n; |
| 482 | str += n; |
| 483 | } |
| 484 | n = strspn(str, controls); |
| 485 | memset(p, '.', n); |
| 486 | p += n; |
| 487 | str += n; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 488 | } |
| 489 | strcpy(p, str); |
| 490 | |
| 491 | /* buf[] holds quarantined version of str */ |
| 492 | |
| 493 | /* Each part of the line that matches has the HIGHLIGHT |
| 494 | and NORMAL escape sequences placed around it. |
| 495 | NB: we regex against line, but insert text |
| 496 | from quarantined copy (buf[]) */ |
| 497 | str = buf; |
| 498 | growline = NULL; |
| 499 | eflags = 0; |
| 500 | goto start; |
| 501 | |
| 502 | while (match_status == 0) { |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 503 | char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL, |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 504 | growline ? : "", |
| 505 | match_structs.rm_so, str, |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 506 | match_structs.rm_eo - match_structs.rm_so, |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 507 | str + match_structs.rm_so); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 508 | free(growline); growline = new; |
| 509 | str += match_structs.rm_eo; |
| 510 | line += match_structs.rm_eo; |
| 511 | eflags = REG_NOTBOL; |
| 512 | start: |
| 513 | /* Most of the time doesn't find the regex, optimize for that */ |
| 514 | match_status = regexec(&pattern, line, 1, &match_structs, eflags); |
| 515 | } |
| 516 | |
| 517 | if (!growline) { |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 518 | printf(CLEAR_2_EOL"%s\n", str); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 519 | return; |
| 520 | } |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 521 | printf(CLEAR_2_EOL"%s%s\n", growline, str); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 522 | free(growline); |
| 523 | } |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 524 | #else |
| 525 | void print_found(const char *line); |
| 526 | #endif |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 527 | |
| 528 | static void print_ascii(const char *str) |
| 529 | { |
| 530 | char buf[width]; |
| 531 | char *p; |
| 532 | size_t n; |
| 533 | |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 534 | printf(CLEAR_2_EOL); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 535 | while (*str) { |
| 536 | n = strcspn(str, controls); |
| 537 | if (n) { |
| 538 | if (!str[n]) break; |
Denis Vlasenko | 806116b | 2006-12-31 12:14:16 +0000 | [diff] [blame] | 539 | printf("%.*s", (int) n, str); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 540 | str += n; |
| 541 | } |
| 542 | n = strspn(str, controls); |
| 543 | p = buf; |
| 544 | do { |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 545 | if (*str == 0x7f) |
| 546 | *p++ = '?'; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 547 | else if (*str == (char)0x9b) |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 548 | /* VT100's CSI, aka Meta-ESC. Who's inventor? */ |
| 549 | /* I want to know who committed this sin */ |
| 550 | *p++ = '{'; |
| 551 | else |
| 552 | *p++ = ctrlconv[(unsigned char)*str]; |
| 553 | str++; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 554 | } while (--n); |
| 555 | *p = '\0'; |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 556 | print_hilite(buf); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 557 | } |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 558 | puts(str); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 561 | /* Print the buffer */ |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 562 | static void buffer_print(void) |
| 563 | { |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 564 | int i; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 565 | |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 566 | move_cursor(0, 0); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 567 | for (i = 0; i <= max_displayed_line; i++) |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 568 | if (pattern_valid) |
| 569 | print_found(buffer[i]); |
| 570 | else |
| 571 | print_ascii(buffer[i]); |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 572 | status_print(); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 575 | static void buffer_fill_and_print(void) |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 576 | { |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 577 | int i; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 578 | for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) { |
| 579 | buffer[i] = flines[cur_fline + i]; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 580 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 581 | for (; i <= max_displayed_line; i++) { |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 582 | buffer[i] = empty_line_marker; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 583 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 584 | buffer_print(); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /* 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] | 588 | static void buffer_down(int nlines) |
| 589 | { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 590 | cur_fline += nlines; |
| 591 | read_lines(); |
Denis Vlasenko | 5c1de36 | 2007-03-08 16:44:32 +0000 | [diff] [blame] | 592 | cap_cur_fline(nlines); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 593 | buffer_fill_and_print(); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 596 | static void buffer_up(int nlines) |
| 597 | { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 598 | cur_fline -= nlines; |
| 599 | if (cur_fline < 0) cur_fline = 0; |
| 600 | read_lines(); |
| 601 | buffer_fill_and_print(); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 604 | static void buffer_line(int linenum) |
| 605 | { |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 606 | if (linenum < 0) |
| 607 | linenum = 0; |
| 608 | cur_fline = linenum; |
| 609 | read_lines(); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 610 | if (linenum + max_displayed_line > max_fline) |
| 611 | linenum = max_fline - max_displayed_line + TILDES; |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 612 | if (linenum < 0) |
| 613 | linenum = 0; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 614 | cur_fline = linenum; |
| 615 | buffer_fill_and_print(); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 618 | static void open_file_and_read_lines(void) |
| 619 | { |
| 620 | if (filename) { |
| 621 | int fd = xopen(filename, O_RDONLY); |
| 622 | dup2(fd, 0); |
| 623 | if (fd) close(fd); |
| 624 | } else { |
| 625 | /* "less" with no arguments in argv[] */ |
| 626 | /* For status line only */ |
| 627 | filename = xstrdup(bb_msg_standard_input); |
| 628 | } |
| 629 | readpos = 0; |
| 630 | readeof = 0; |
| 631 | linepos = 0; |
| 632 | terminated = 1; |
| 633 | read_lines(); |
| 634 | } |
| 635 | |
| 636 | /* Reinitialize everything for a new file - free the memory and start over */ |
| 637 | static void reinitialize(void) |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 638 | { |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 639 | int i; |
| 640 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 641 | if (flines) { |
| 642 | for (i = 0; i <= max_fline; i++) |
| 643 | free((void*)(flines[i])); |
| 644 | free(flines); |
| 645 | flines = NULL; |
| 646 | } |
"Vladimir N. Oleynik" | a0ae6de | 2005-09-19 10:28:43 +0000 | [diff] [blame] | 647 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 648 | max_fline = -1; |
| 649 | cur_fline = 0; |
| 650 | max_lineno = 0; |
| 651 | open_file_and_read_lines(); |
| 652 | buffer_fill_and_print(); |
| 653 | } |
| 654 | |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 655 | static void getch_nowait(char* input, int sz) |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 656 | { |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 657 | ssize_t rd; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 658 | fd_set readfds; |
| 659 | again: |
| 660 | fflush(stdout); |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 661 | |
| 662 | /* NB: select returns whenever read will not block. Therefore: |
| 663 | * (a) with O_NONBLOCK'ed fds select will return immediately |
| 664 | * (b) if eof is reached, select will also return |
| 665 | * because read will immediately return 0 bytes. |
| 666 | * Even if select says that input is available, read CAN block |
| 667 | * (switch fd into O_NONBLOCK'ed mode to avoid it) |
| 668 | */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 669 | FD_ZERO(&readfds); |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 670 | if (max_fline <= cur_fline + max_displayed_line |
| 671 | && eof_error > 0 /* did NOT reach eof yet */ |
| 672 | ) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 673 | /* We are interested in stdin */ |
| 674 | FD_SET(0, &readfds); |
| 675 | } |
| 676 | FD_SET(kbd_fd, &readfds); |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 677 | tcsetattr(kbd_fd, TCSANOW, &term_less); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 678 | select(kbd_fd + 1, &readfds, NULL, NULL, NULL); |
| 679 | |
| 680 | input[0] = '\0'; |
| 681 | ndelay_on(kbd_fd); |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 682 | rd = read(kbd_fd, input, sz); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 683 | ndelay_off(kbd_fd); |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 684 | if (rd < 0) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 685 | /* No keyboard input, but we have input on stdin! */ |
| 686 | if (errno != EAGAIN) /* Huh?? */ |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 687 | return; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 688 | read_lines(); |
| 689 | buffer_fill_and_print(); |
| 690 | goto again; |
| 691 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* Grab a character from input without requiring the return key. If the |
Denis Vlasenko | 7cea262 | 2006-12-24 07:30:09 +0000 | [diff] [blame] | 695 | * character is ASCII \033, get more characters and assign certain sequences |
| 696 | * special return codes. Note that this function works best with raw input. */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 697 | static int less_getch(void) |
| 698 | { |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 699 | char input[16]; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 700 | unsigned i; |
| 701 | again: |
Denis Vlasenko | 46340e3 | 2007-08-03 14:17:21 +0000 | [diff] [blame] | 702 | memset(input, 0, sizeof(input)); |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 703 | getch_nowait(input, sizeof(input)); |
Denis Vlasenko | 46340e3 | 2007-08-03 14:17:21 +0000 | [diff] [blame] | 704 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 705 | /* Detect escape sequences (i.e. arrow keys) and handle |
| 706 | * them accordingly */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 707 | if (input[0] == '\033' && input[1] == '[') { |
| 708 | set_tty_cooked(); |
| 709 | i = input[2] - REAL_KEY_UP; |
| 710 | if (i < 4) |
| 711 | return 20 + i; |
| 712 | i = input[2] - REAL_PAGE_UP; |
| 713 | if (i < 4) |
| 714 | return 24 + i; |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 715 | if (input[2] == REAL_KEY_HOME_XTERM) |
| 716 | return KEY_HOME; |
Denis Vlasenko | f5a1576 | 2007-03-09 08:55:23 +0000 | [diff] [blame] | 717 | if (input[2] == REAL_KEY_HOME_ALT) |
| 718 | return KEY_HOME; |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 719 | if (input[2] == REAL_KEY_END_XTERM) |
| 720 | return KEY_END; |
Denis Vlasenko | f5a1576 | 2007-03-09 08:55:23 +0000 | [diff] [blame] | 721 | if (input[2] == REAL_KEY_END_ALT) |
| 722 | return KEY_END; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 723 | return 0; |
| 724 | } |
| 725 | /* Reject almost all control chars */ |
| 726 | i = input[0]; |
| 727 | if (i < ' ' && i != 0x0d && i != 8) goto again; |
| 728 | set_tty_cooked(); |
| 729 | return i; |
| 730 | } |
| 731 | |
| 732 | static char* less_gets(int sz) |
| 733 | { |
| 734 | char c; |
| 735 | int i = 0; |
| 736 | char *result = xzalloc(1); |
| 737 | while (1) { |
| 738 | fflush(stdout); |
| 739 | |
| 740 | /* I be damned if I know why is it needed *repeatedly*, |
| 741 | * but it is needed. Is it because of stdio? */ |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 742 | tcsetattr(kbd_fd, TCSANOW, &term_less); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 743 | |
Denis Vlasenko | 46340e3 | 2007-08-03 14:17:21 +0000 | [diff] [blame] | 744 | c = '\0'; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 745 | read(kbd_fd, &c, 1); |
| 746 | if (c == 0x0d) |
| 747 | return result; |
| 748 | if (c == 0x7f) |
| 749 | c = 8; |
| 750 | if (c == 8 && i) { |
| 751 | printf("\x8 \x8"); |
| 752 | i--; |
| 753 | } |
| 754 | if (c < ' ') |
| 755 | continue; |
| 756 | if (i >= width - sz - 1) |
| 757 | continue; /* len limit */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 758 | bb_putchar(c); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 759 | result[i++] = c; |
| 760 | result = xrealloc(result, i+1); |
| 761 | result[i] = '\0'; |
| 762 | } |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 765 | static void examine_file(void) |
| 766 | { |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 767 | print_statusline("Examine: "); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 768 | free(filename); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 769 | filename = less_gets(sizeof("Examine: ")-1); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 770 | /* files start by = argv. why we assume that argv is infinitely long?? |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 771 | files[num_files] = filename; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 772 | current_file = num_files + 1; |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 773 | num_files++; */ |
| 774 | files[0] = filename; |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 775 | num_files = current_file = 1; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 776 | reinitialize(); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 779 | /* This function changes the file currently being paged. direction can be one of the following: |
| 780 | * -1: go back one file |
| 781 | * 0: go to the first file |
Denis Vlasenko | 7cea262 | 2006-12-24 07:30:09 +0000 | [diff] [blame] | 782 | * 1: go forward one file */ |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 783 | static void change_file(int direction) |
| 784 | { |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 785 | if (current_file != ((direction > 0) ? num_files : 1)) { |
| 786 | current_file = direction ? current_file + direction : 1; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 787 | free(filename); |
| 788 | filename = xstrdup(files[current_file - 1]); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 789 | reinitialize(); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 790 | } else { |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 791 | print_statusline(direction > 0 ? "No next file" : "No previous file"); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 795 | static void remove_current_file(void) |
| 796 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 797 | int i; |
| 798 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 799 | if (num_files < 2) |
| 800 | return; |
| 801 | |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 802 | if (current_file != 1) { |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 803 | change_file(-1); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 804 | for (i = 3; i <= num_files; i++) |
| 805 | files[i - 2] = files[i - 1]; |
| 806 | num_files--; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 807 | } else { |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 808 | change_file(1); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 809 | for (i = 2; i <= num_files; i++) |
| 810 | files[i - 2] = files[i - 1]; |
| 811 | num_files--; |
| 812 | current_file--; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 816 | static void colon_process(void) |
| 817 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 818 | int keypress; |
| 819 | |
| 820 | /* Clear the current line and print a prompt */ |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 821 | print_statusline(" :"); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 822 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 823 | keypress = less_getch(); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 824 | switch (keypress) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 825 | case 'd': |
| 826 | remove_current_file(); |
| 827 | break; |
| 828 | case 'e': |
| 829 | examine_file(); |
| 830 | break; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 831 | #if ENABLE_FEATURE_LESS_FLAGS |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 832 | case 'f': |
| 833 | m_status_print(); |
| 834 | break; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 835 | #endif |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 836 | case 'n': |
| 837 | change_file(1); |
| 838 | break; |
| 839 | case 'p': |
| 840 | change_file(-1); |
| 841 | break; |
| 842 | case 'q': |
| 843 | less_exit(0); |
| 844 | break; |
| 845 | case 'x': |
| 846 | change_file(0); |
| 847 | break; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 851 | #if ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 852 | static void normalize_match_pos(int match) |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 853 | { |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 854 | if (match >= num_matches) |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 855 | match = num_matches - 1; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 856 | if (match < 0) |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 857 | match = 0; |
| 858 | match_pos = match; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Rob Landley | a2e9804 | 2006-04-18 01:53:41 +0000 | [diff] [blame] | 861 | static void goto_match(int match) |
| 862 | { |
Denis Vlasenko | 8465a99 | 2007-05-09 18:32:54 +0000 | [diff] [blame] | 863 | int sv; |
| 864 | |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 865 | if (!pattern_valid) |
| 866 | return; |
| 867 | if (match < 0) |
| 868 | match = 0; |
Denis Vlasenko | 8465a99 | 2007-05-09 18:32:54 +0000 | [diff] [blame] | 869 | sv = cur_fline; |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 870 | /* Try to find next match if eof isn't reached yet */ |
| 871 | if (match >= num_matches && eof_error > 0) { |
| 872 | cur_fline = MAXLINES; /* look as far as needed */ |
| 873 | read_lines(); |
| 874 | } |
| 875 | if (num_matches) { |
Denis Vlasenko | 8465a99 | 2007-05-09 18:32:54 +0000 | [diff] [blame] | 876 | cap_cur_fline(cur_fline); |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 877 | normalize_match_pos(match); |
| 878 | buffer_line(match_lines[match_pos]); |
Denis Vlasenko | 8465a99 | 2007-05-09 18:32:54 +0000 | [diff] [blame] | 879 | } else { |
| 880 | cur_fline = sv; |
| 881 | print_statusline("No matches found"); |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 882 | } |
Rob Landley | a2e9804 | 2006-04-18 01:53:41 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 885 | static void fill_match_lines(unsigned pos) |
| 886 | { |
| 887 | if (!pattern_valid) |
| 888 | return; |
| 889 | /* Run the regex on each line of the current file */ |
| 890 | while (pos <= max_fline) { |
| 891 | /* If this line matches */ |
| 892 | if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0 |
| 893 | /* and we didn't match it last time */ |
| 894 | && !(num_matches && match_lines[num_matches-1] == pos) |
| 895 | ) { |
| 896 | match_lines = xrealloc(match_lines, (num_matches+1) * sizeof(int)); |
| 897 | match_lines[num_matches++] = pos; |
| 898 | } |
| 899 | pos++; |
| 900 | } |
| 901 | } |
| 902 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 903 | static void regex_process(void) |
| 904 | { |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 905 | char *uncomp_regex, *err; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 906 | |
| 907 | /* Reset variables */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 908 | free(match_lines); |
| 909 | match_lines = NULL; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 910 | match_pos = 0; |
| 911 | num_matches = 0; |
| 912 | if (pattern_valid) { |
| 913 | regfree(&pattern); |
| 914 | pattern_valid = 0; |
| 915 | } |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 916 | |
Rob Landley | a2e9804 | 2006-04-18 01:53:41 +0000 | [diff] [blame] | 917 | /* Get the uncompiled regular expression from the user */ |
| 918 | clear_line(); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 919 | bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/'); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 920 | uncomp_regex = less_gets(1); |
| 921 | if (!uncomp_regex[0]) { |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 922 | free(uncomp_regex); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 923 | buffer_print(); |
Rob Landley | a2e9804 | 2006-04-18 01:53:41 +0000 | [diff] [blame] | 924 | return; |
| 925 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 926 | |
Rob Landley | a2e9804 | 2006-04-18 01:53:41 +0000 | [diff] [blame] | 927 | /* Compile the regex and check for errors */ |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 928 | err = regcomp_or_errmsg(&pattern, uncomp_regex, 0); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 929 | free(uncomp_regex); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 930 | if (err) { |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 931 | print_statusline(err); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 932 | free(err); |
| 933 | return; |
| 934 | } |
Denis Vlasenko | a1c6312 | 2007-03-08 18:12:01 +0000 | [diff] [blame] | 935 | |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 936 | pattern_valid = 1; |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 937 | match_pos = 0; |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 938 | fill_match_lines(0); |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 939 | while (match_pos < num_matches) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 940 | if (match_lines[match_pos] > cur_fline) |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 941 | break; |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 942 | match_pos++; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 943 | } |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 944 | if (option_mask32 & LESS_STATE_MATCH_BACKWARDS) |
| 945 | match_pos--; |
Denis Vlasenko | a1c6312 | 2007-03-08 18:12:01 +0000 | [diff] [blame] | 946 | |
| 947 | /* It's possible that no matches are found yet. |
| 948 | * goto_match() will read input looking for match, |
| 949 | * if needed */ |
| 950 | goto_match(match_pos); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 951 | } |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 952 | #endif |
| 953 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 954 | static void number_process(int first_digit) |
| 955 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 956 | int i = 1; |
| 957 | int num; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 958 | char num_input[sizeof(int)*4]; /* more than enough */ |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 959 | char keypress; |
"Vladimir N. Oleynik" | bc37480 | 2005-09-19 14:23:46 +0000 | [diff] [blame] | 960 | |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 961 | num_input[0] = first_digit; |
| 962 | |
| 963 | /* Clear the current line, print a prompt, and then print the digit */ |
| 964 | clear_line(); |
| 965 | printf(":%c", first_digit); |
| 966 | |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 967 | /* Receive input until a letter is given */ |
| 968 | while (i < sizeof(num_input)-1) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 969 | num_input[i] = less_getch(); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 970 | if (!num_input[i] || !isdigit(num_input[i])) |
| 971 | break; |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 972 | bb_putchar(num_input[i]); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 973 | i++; |
| 974 | } |
| 975 | |
| 976 | /* Take the final letter out of the digits string */ |
| 977 | keypress = num_input[i]; |
| 978 | num_input[i] = '\0'; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 979 | num = bb_strtou(num_input, NULL, 10); |
| 980 | /* on format error, num == -1 */ |
| 981 | if (num < 1 || num > MAXLINES) { |
Rob Landley | a2e9804 | 2006-04-18 01:53:41 +0000 | [diff] [blame] | 982 | buffer_print(); |
| 983 | return; |
| 984 | } |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 985 | |
| 986 | /* We now know the number and the letter entered, so we process them */ |
| 987 | switch (keypress) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 988 | case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015': |
| 989 | buffer_down(num); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 990 | break; |
| 991 | case KEY_UP: case 'b': case 'w': case 'y': case 'u': |
| 992 | buffer_up(num); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 993 | break; |
| 994 | case 'g': case '<': case 'G': case '>': |
| 995 | cur_fline = num + max_displayed_line; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 996 | read_lines(); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 997 | buffer_line(num - 1); |
| 998 | break; |
| 999 | case 'p': case '%': |
| 1000 | num = num * (max_fline / 100); /* + max_fline / 2; */ |
| 1001 | cur_fline = num + max_displayed_line; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 1002 | read_lines(); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1003 | buffer_line(num); |
| 1004 | break; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1005 | #if ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1006 | case 'n': |
| 1007 | goto_match(match_pos + num); |
| 1008 | break; |
| 1009 | case '/': |
| 1010 | option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS; |
| 1011 | regex_process(); |
| 1012 | break; |
| 1013 | case '?': |
| 1014 | option_mask32 |= LESS_STATE_MATCH_BACKWARDS; |
| 1015 | regex_process(); |
| 1016 | break; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1017 | #endif |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1021 | #if ENABLE_FEATURE_LESS_FLAGCS |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1022 | static void flag_change(void) |
| 1023 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1024 | int keypress; |
| 1025 | |
| 1026 | clear_line(); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1027 | bb_putchar('-'); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1028 | keypress = less_getch(); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1029 | |
| 1030 | switch (keypress) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1031 | case 'M': |
| 1032 | option_mask32 ^= FLAG_M; |
| 1033 | break; |
| 1034 | case 'm': |
| 1035 | option_mask32 ^= FLAG_m; |
| 1036 | break; |
| 1037 | case 'E': |
| 1038 | option_mask32 ^= FLAG_E; |
| 1039 | break; |
| 1040 | case '~': |
| 1041 | option_mask32 ^= FLAG_TILDE; |
| 1042 | break; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1046 | static void show_flag_status(void) |
| 1047 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1048 | int keypress; |
| 1049 | int flag_val; |
| 1050 | |
| 1051 | clear_line(); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1052 | bb_putchar('_'); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1053 | keypress = less_getch(); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1054 | |
| 1055 | switch (keypress) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1056 | case 'M': |
| 1057 | flag_val = option_mask32 & FLAG_M; |
| 1058 | break; |
| 1059 | case 'm': |
| 1060 | flag_val = option_mask32 & FLAG_m; |
| 1061 | break; |
| 1062 | case '~': |
| 1063 | flag_val = option_mask32 & FLAG_TILDE; |
| 1064 | break; |
| 1065 | case 'N': |
| 1066 | flag_val = option_mask32 & FLAG_N; |
| 1067 | break; |
| 1068 | case 'E': |
| 1069 | flag_val = option_mask32 & FLAG_E; |
| 1070 | break; |
| 1071 | default: |
| 1072 | flag_val = 0; |
| 1073 | break; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | clear_line(); |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1077 | 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] | 1078 | } |
| 1079 | #endif |
| 1080 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1081 | static void save_input_to_file(void) |
| 1082 | { |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1083 | const char *msg = ""; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1084 | char *current_line; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1085 | int i; |
| 1086 | FILE *fp; |
| 1087 | |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1088 | print_statusline("Log file: "); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1089 | current_line = less_gets(sizeof("Log file: ")-1); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1090 | if (strlen(current_line) > 0) { |
| 1091 | fp = fopen(current_line, "w"); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1092 | if (!fp) { |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1093 | msg = "Error opening log file"; |
| 1094 | goto ret; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1095 | } |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1096 | for (i = 0; i <= max_fline; i++) |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1097 | fprintf(fp, "%s\n", flines[i]); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1098 | fclose(fp); |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1099 | msg = "Done"; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1100 | } |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1101 | ret: |
| 1102 | print_statusline(msg); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1103 | free(current_line); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1106 | #if ENABLE_FEATURE_LESS_MARKS |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1107 | static void add_mark(void) |
| 1108 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1109 | int letter; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1110 | |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1111 | print_statusline("Mark: "); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1112 | letter = less_getch(); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1113 | |
| 1114 | if (isalpha(letter)) { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1115 | /* If we exceed 15 marks, start overwriting previous ones */ |
| 1116 | if (num_marks == 14) |
| 1117 | num_marks = 0; |
| 1118 | |
| 1119 | mark_lines[num_marks][0] = letter; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1120 | mark_lines[num_marks][1] = cur_fline; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1121 | num_marks++; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1122 | } else { |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1123 | print_statusline("Invalid mark letter"); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1127 | static void goto_mark(void) |
| 1128 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1129 | int letter; |
| 1130 | int i; |
| 1131 | |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1132 | print_statusline("Go to mark: "); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1133 | letter = less_getch(); |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 1134 | clear_line(); |
"Vladimir N. Oleynik" | a0ae6de | 2005-09-19 10:28:43 +0000 | [diff] [blame] | 1135 | |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1136 | if (isalpha(letter)) { |
| 1137 | for (i = 0; i <= num_marks; i++) |
| 1138 | if (letter == mark_lines[i][0]) { |
| 1139 | buffer_line(mark_lines[i][1]); |
| 1140 | break; |
| 1141 | } |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1142 | if (num_marks == 14 && letter != mark_lines[14][0]) |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1143 | print_statusline("Mark not set"); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1144 | } else |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1145 | print_statusline("Invalid mark letter"); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1146 | } |
| 1147 | #endif |
| 1148 | |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1149 | #if ENABLE_FEATURE_LESS_BRACKETS |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1150 | static char opp_bracket(char bracket) |
| 1151 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1152 | switch (bracket) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1153 | case '{': case '[': |
| 1154 | return bracket + 2; |
| 1155 | case '(': |
| 1156 | return ')'; |
| 1157 | case '}': case ']': |
| 1158 | return bracket - 2; |
| 1159 | case ')': |
| 1160 | return '('; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1161 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1162 | return 0; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1165 | static void match_right_bracket(char bracket) |
| 1166 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1167 | int bracket_line = -1; |
| 1168 | int i; |
| 1169 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1170 | if (strchr(flines[cur_fline], bracket) == NULL) { |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1171 | print_statusline("No bracket in top line"); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1172 | return; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1173 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1174 | for (i = cur_fline + 1; i < max_fline; i++) { |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1175 | if (strchr(flines[i], opp_bracket(bracket)) != NULL) { |
| 1176 | bracket_line = i; |
| 1177 | break; |
| 1178 | } |
| 1179 | } |
| 1180 | if (bracket_line == -1) |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1181 | print_statusline("No matching bracket found"); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1182 | buffer_line(bracket_line - max_displayed_line); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1185 | static void match_left_bracket(char bracket) |
| 1186 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1187 | int bracket_line = -1; |
| 1188 | int i; |
| 1189 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1190 | if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) { |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1191 | print_statusline("No bracket in bottom line"); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1192 | return; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1193 | } |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1194 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1195 | for (i = cur_fline + max_displayed_line; i >= 0; i--) { |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1196 | if (strchr(flines[i], opp_bracket(bracket)) != NULL) { |
| 1197 | bracket_line = i; |
| 1198 | break; |
| 1199 | } |
| 1200 | } |
| 1201 | if (bracket_line == -1) |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1202 | print_statusline("No matching bracket found"); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1203 | buffer_line(bracket_line); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1204 | } |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1205 | #endif /* FEATURE_LESS_BRACKETS */ |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1206 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1207 | static void keypress_process(int keypress) |
| 1208 | { |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1209 | switch (keypress) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1210 | case KEY_DOWN: case 'e': case 'j': case 0x0d: |
| 1211 | buffer_down(1); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1212 | break; |
| 1213 | case KEY_UP: case 'y': case 'k': |
| 1214 | buffer_up(1); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1215 | break; |
Denis Vlasenko | 78e2d4e | 2007-09-27 17:11:48 +0000 | [diff] [blame] | 1216 | case PAGE_DOWN: case ' ': case 'z': case 'f': |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1217 | buffer_down(max_displayed_line + 1); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1218 | break; |
| 1219 | case PAGE_UP: case 'w': case 'b': |
| 1220 | buffer_up(max_displayed_line + 1); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1221 | break; |
| 1222 | case 'd': |
| 1223 | buffer_down((max_displayed_line + 1) / 2); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1224 | break; |
| 1225 | case 'u': |
| 1226 | buffer_up((max_displayed_line + 1) / 2); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1227 | break; |
| 1228 | case KEY_HOME: case 'g': case 'p': case '<': case '%': |
| 1229 | buffer_line(0); |
| 1230 | break; |
| 1231 | case KEY_END: case 'G': case '>': |
| 1232 | cur_fline = MAXLINES; |
| 1233 | read_lines(); |
| 1234 | buffer_line(cur_fline); |
| 1235 | break; |
| 1236 | case 'q': case 'Q': |
| 1237 | less_exit(0); |
| 1238 | break; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1239 | #if ENABLE_FEATURE_LESS_MARKS |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1240 | case 'm': |
| 1241 | add_mark(); |
| 1242 | buffer_print(); |
| 1243 | break; |
| 1244 | case '\'': |
| 1245 | goto_mark(); |
| 1246 | buffer_print(); |
| 1247 | break; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1248 | #endif |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1249 | case 'r': case 'R': |
| 1250 | buffer_print(); |
| 1251 | break; |
| 1252 | /*case 'R': |
| 1253 | full_repaint(); |
| 1254 | break;*/ |
| 1255 | case 's': |
| 1256 | save_input_to_file(); |
| 1257 | break; |
| 1258 | case 'E': |
| 1259 | examine_file(); |
| 1260 | break; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1261 | #if ENABLE_FEATURE_LESS_FLAGS |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1262 | case '=': |
| 1263 | m_status_print(); |
| 1264 | break; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1265 | #endif |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1266 | #if ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1267 | case '/': |
| 1268 | option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS; |
| 1269 | regex_process(); |
| 1270 | break; |
| 1271 | case 'n': |
| 1272 | goto_match(match_pos + 1); |
| 1273 | break; |
| 1274 | case 'N': |
| 1275 | goto_match(match_pos - 1); |
| 1276 | break; |
| 1277 | case '?': |
| 1278 | option_mask32 |= LESS_STATE_MATCH_BACKWARDS; |
| 1279 | regex_process(); |
| 1280 | break; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1281 | #endif |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1282 | #if ENABLE_FEATURE_LESS_FLAGCS |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1283 | case '-': |
| 1284 | flag_change(); |
| 1285 | buffer_print(); |
| 1286 | break; |
| 1287 | case '_': |
| 1288 | show_flag_status(); |
| 1289 | break; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1290 | #endif |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1291 | #if ENABLE_FEATURE_LESS_BRACKETS |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1292 | case '{': case '(': case '[': |
| 1293 | match_right_bracket(keypress); |
| 1294 | break; |
| 1295 | case '}': case ')': case ']': |
| 1296 | match_left_bracket(keypress); |
| 1297 | break; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1298 | #endif |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1299 | case ':': |
| 1300 | colon_process(); |
| 1301 | break; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1302 | } |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 1303 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1304 | if (isdigit(keypress)) |
| 1305 | number_process(keypress); |
| 1306 | } |
| 1307 | |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1308 | static void sig_catcher(int sig ATTRIBUTE_UNUSED) |
| 1309 | { |
| 1310 | set_tty_cooked(); |
| 1311 | exit(1); |
| 1312 | } |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1313 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 1314 | int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1315 | int less_main(int argc, char **argv) |
| 1316 | { |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1317 | int keypress; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1318 | |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 1319 | INIT_G(); |
| 1320 | |
Denis Vlasenko | 95b3071 | 2006-12-31 19:23:31 +0000 | [diff] [blame] | 1321 | /* TODO: -x: do not interpret backspace, -xx: tab also */ |
| 1322 | /* -xxx: newline also */ |
| 1323 | /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 1324 | getopt32(argv, "EMmN~"); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1325 | argc -= optind; |
| 1326 | argv += optind; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1327 | num_files = argc; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1328 | files = argv; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1329 | |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1330 | /* Another popular pager, most, detects when stdout |
| 1331 | * is not a tty and turns into cat. This makes sense. */ |
| 1332 | if (!isatty(STDOUT_FILENO)) |
| 1333 | return bb_cat(argv); |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 1334 | kbd_fd = open(CURRENT_TTY, O_RDONLY); |
| 1335 | if (kbd_fd < 0) |
| 1336 | return bb_cat(argv); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1337 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1338 | if (!num_files) { |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1339 | if (isatty(STDIN_FILENO)) { |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1340 | /* Just "less"? No args and no redirection? */ |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 1341 | bb_error_msg("missing filename"); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1342 | bb_show_usage(); |
| 1343 | } |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1344 | } else |
| 1345 | filename = xstrdup(files[0]); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1346 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1347 | get_terminal_width_height(kbd_fd, &width, &max_displayed_line); |
| 1348 | /* 20: two tabstops + 4 */ |
| 1349 | if (width < 20 || max_displayed_line < 3) |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 1350 | bb_error_msg_and_die("too narrow here"); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1351 | max_displayed_line -= 2; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 1352 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1353 | buffer = xmalloc((max_displayed_line+1) * sizeof(char *)); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1354 | if (option_mask32 & FLAG_TILDE) |
| 1355 | empty_line_marker = ""; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 1356 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1357 | tcgetattr(kbd_fd, &term_orig); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1358 | signal(SIGTERM, sig_catcher); |
| 1359 | signal(SIGINT, sig_catcher); |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 1360 | term_less = term_orig; |
| 1361 | term_less.c_lflag &= ~(ICANON | ECHO); |
| 1362 | term_less.c_iflag &= ~(IXON | ICRNL); |
| 1363 | /*term_less.c_oflag &= ~ONLCR;*/ |
| 1364 | term_less.c_cc[VMIN] = 1; |
| 1365 | term_less.c_cc[VTIME] = 0; |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1366 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1367 | /* Want to do it just once, but it doesn't work, */ |
| 1368 | /* so we are redoing it (see code above). Mystery... */ |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 1369 | /*tcsetattr(kbd_fd, TCSANOW, &term_less);*/ |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1370 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1371 | reinitialize(); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1372 | while (1) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1373 | keypress = less_getch(); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1374 | keypress_process(keypress); |
| 1375 | } |
| 1376 | } |