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