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