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