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