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