diff options
| author | "Vladimir N. Oleynik" <dzo@simtreas.ru> | 2005-09-16 12:32:22 +0000 |
|---|---|---|
| committer | "Vladimir N. Oleynik" <dzo@simtreas.ru> | 2005-09-16 12:32:22 +0000 |
| commit | 2b306e906f6ba9fe990365a0faa00140d7f4dbe1 (patch) | |
| tree | b9a6f0177eff6b75628111f776c8410db444529b /miscutils | |
| parent | 8552aec7fd4380791f44cba4417418e7b1cfb195 (diff) | |
| download | busybox-w32-2b306e906f6ba9fe990365a0faa00140d7f4dbe1.tar.gz busybox-w32-2b306e906f6ba9fe990365a0faa00140d7f4dbe1.tar.bz2 busybox-w32-2b306e906f6ba9fe990365a0faa00140d7f4dbe1.zip | |
small change for highlihting flags, more busyboxes: indent, forward declaration, paste BSS
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/less.c | 974 |
1 files changed, 464 insertions, 510 deletions
diff --git a/miscutils/less.c b/miscutils/less.c index 43bc6737e..99149a51d 100644 --- a/miscutils/less.c +++ b/miscutils/less.c | |||
| @@ -23,7 +23,7 @@ | |||
| 23 | * This program needs a lot of development, so consider it in a beta stage | 23 | * This program needs a lot of development, so consider it in a beta stage |
| 24 | * at best. | 24 | * at best. |
| 25 | * | 25 | * |
| 26 | * TODO: | 26 | * TODO: |
| 27 | * - Add more regular expression support - search modifiers, certain matches, etc. | 27 | * - Add more regular expression support - search modifiers, certain matches, etc. |
| 28 | * - Add more complex bracket searching - currently, nested brackets are | 28 | * - Add more complex bracket searching - currently, nested brackets are |
| 29 | * not considered. | 29 | * not considered. |
| @@ -47,10 +47,15 @@ | |||
| 47 | #include <string.h> | 47 | #include <string.h> |
| 48 | #include <termios.h> | 48 | #include <termios.h> |
| 49 | #include <unistd.h> | 49 | #include <unistd.h> |
| 50 | #include <regex.h> | ||
| 51 | #include <ctype.h> | 50 | #include <ctype.h> |
| 51 | |||
| 52 | #include "busybox.h" | 52 | #include "busybox.h" |
| 53 | 53 | ||
| 54 | #ifdef CONFIG_FEATURE_LESS_REGEXP | ||
| 55 | #include "xregex.h" | ||
| 56 | #endif | ||
| 57 | |||
| 58 | |||
| 54 | /* These are the escape sequences corresponding to special keys */ | 59 | /* These are the escape sequences corresponding to special keys */ |
| 55 | #define REAL_KEY_UP 'A' | 60 | #define REAL_KEY_UP 'A' |
| 56 | #define REAL_KEY_DOWN 'B' | 61 | #define REAL_KEY_DOWN 'B' |
| @@ -78,63 +83,7 @@ | |||
| 78 | #define MAXLINES 10000 | 83 | #define MAXLINES 10000 |
| 79 | 84 | ||
| 80 | /* Get height and width of terminal */ | 85 | /* Get height and width of terminal */ |
| 81 | #define tty_width_height() get_terminal_width_height(0, &width, &height) | 86 | #define tty_width_height() get_terminal_width_height(0, &width, &height) |
| 82 | |||
| 83 | /* Function prototypes */ | ||
| 84 | static void set_tty_cooked(void); | ||
| 85 | static void set_tty_raw(void); | ||
| 86 | static void tless_exit(int code); | ||
| 87 | static int tless_getch(void); | ||
| 88 | static void move_cursor(int x, int y); | ||
| 89 | static void clear_line(void); | ||
| 90 | static void data_readlines(void); | ||
| 91 | static void free_flines(void); | ||
| 92 | #ifdef CONFIG_FEATURE_LESS_FLAGS | ||
| 93 | static int calc_percent(void); | ||
| 94 | #endif | ||
| 95 | static int reverse_percent(int percentage); | ||
| 96 | #ifdef CONFIG_FEATURE_LESS_FLAGS | ||
| 97 | static void m_status_print(void); | ||
| 98 | static void medium_status_print(void); | ||
| 99 | #endif | ||
| 100 | static void status_print(void); | ||
| 101 | static void buffer_print(void); | ||
| 102 | static void buffer_init(void); | ||
| 103 | static void buffer_down(int nlines); | ||
| 104 | static void buffer_up(int nlines); | ||
| 105 | static void buffer_line(int linenum); | ||
| 106 | static void keypress_process(int keypress); | ||
| 107 | static void colon_process(void); | ||
| 108 | static void number_process(int first_digit); | ||
| 109 | #ifdef CONFIG_FEATURE_LESS_FLAGCS | ||
| 110 | static void flag_change(void); | ||
| 111 | static void show_flag_status(void); | ||
| 112 | #endif | ||
| 113 | static void examine_file(void); | ||
| 114 | static void next_file(void); | ||
| 115 | static void previous_file(void); | ||
| 116 | static void first_file(void); | ||
| 117 | static void remove_current_file(void); | ||
| 118 | static void full_repaint(void); | ||
| 119 | static void add_linenumbers(void); | ||
| 120 | static void save_input_to_file(void); | ||
| 121 | #ifdef CONFIG_FEATURE_LESS_MARKS | ||
| 122 | static void add_mark(void); | ||
| 123 | static void goto_mark(void); | ||
| 124 | #endif | ||
| 125 | #ifdef CONFIG_FEATURE_LESS_REGEXP | ||
| 126 | static void regex_process(void); | ||
| 127 | char *process_regex_on_line(char *line, regex_t *pattern); | ||
| 128 | char *insert_highlights(char *line, int start, int end); | ||
| 129 | static void goto_match (int match); | ||
| 130 | static void search_backwards(void); | ||
| 131 | #endif | ||
| 132 | #ifdef CONFIG_FEATURE_LESS_BRACKETS | ||
| 133 | static char opp_bracket (char bracket); | ||
| 134 | static void match_right_bracket (char bracket); | ||
| 135 | static void match_left_bracket (char bracket); | ||
| 136 | #endif | ||
| 137 | int less_main(int argc, char *argv[]); | ||
| 138 | 87 | ||
| 139 | static int height; | 88 | static int height; |
| 140 | static int width; | 89 | static int width; |
| @@ -143,38 +92,38 @@ static char filename[256]; | |||
| 143 | static char buffer[100][256]; | 92 | static char buffer[100][256]; |
| 144 | static char *flines[MAXLINES]; | 93 | static char *flines[MAXLINES]; |
| 145 | static int current_file = 1; | 94 | static int current_file = 1; |
| 146 | static int line_pos = 0; | 95 | static int line_pos; |
| 147 | static int num_flines; | 96 | static int num_flines; |
| 148 | static int num_files = 1; | 97 | static int num_files = 1; |
| 149 | static int past_eof = 0; | 98 | static int past_eof; |
| 150 | 99 | ||
| 151 | /* Command line options */ | 100 | /* Command line options */ |
| 152 | static int E_FLAG = 0; | 101 | static int E_FLAG; |
| 153 | static int M_FLAG = 0; | 102 | static int M_FLAG; |
| 154 | static int N_FLAG = 0; | 103 | static int N_FLAG; |
| 155 | static int m_FLAG = 0; | 104 | static int m_FLAG; |
| 156 | static int TILDE_FLAG = 0; | 105 | static int TILDE_FLAG; |
| 157 | 106 | ||
| 158 | /* This is needed so that program behaviour changes when input comes from | 107 | /* This is needed so that program behaviour changes when input comes from |
| 159 | stdin */ | 108 | stdin */ |
| 160 | static int inp_stdin = 0; | 109 | static int inp_stdin; |
| 161 | /* This is required so that when a file is requested to be examined after | 110 | /* This is required so that when a file is requested to be examined after |
| 162 | input has come from stdin (e.g. dmesg | less), the input stream from | 111 | input has come from stdin (e.g. dmesg | less), the input stream from |
| 163 | the keyboard still stays the same. If it switched back to stdin, keyboard | 112 | the keyboard still stays the same. If it switched back to stdin, keyboard |
| 164 | input wouldn't work. */ | 113 | input wouldn't work. */ |
| 165 | static int ea_inp_stdin = 0; | 114 | static int ea_inp_stdin; |
| 166 | 115 | ||
| 167 | #ifdef CONFIG_FEATURE_LESS_MARKS | 116 | #ifdef CONFIG_FEATURE_LESS_MARKS |
| 168 | static int mark_lines[15][2]; | 117 | static int mark_lines[15][2]; |
| 169 | static int num_marks = 0; | 118 | static int num_marks; |
| 170 | #endif | 119 | #endif |
| 171 | 120 | ||
| 172 | #ifdef CONFIG_FEATURE_LESS_REGEXP | 121 | #ifdef CONFIG_FEATURE_LESS_REGEXP |
| 173 | static int match_found = 0; | 122 | static int match_found; |
| 174 | static int match_lines[100]; | 123 | static int match_lines[100]; |
| 175 | static int match_pos = 0; | 124 | static int match_pos; |
| 176 | static int num_matches = 0; | 125 | static int num_matches; |
| 177 | static int match_backwards = 0; | 126 | static int match_backwards; |
| 178 | static int num_back_match = 1; | 127 | static int num_back_match = 1; |
| 179 | #endif | 128 | #endif |
| 180 | 129 | ||
| @@ -185,47 +134,47 @@ static struct termios term_orig, term_vi; | |||
| 185 | static FILE *inp; | 134 | static FILE *inp; |
| 186 | 135 | ||
| 187 | /* Reset terminal input to normal */ | 136 | /* Reset terminal input to normal */ |
| 188 | static void set_tty_cooked() { | 137 | static void set_tty_cooked(void) { |
| 189 | fflush(stdout); | 138 | fflush(stdout); |
| 190 | tcsetattr(0, TCSANOW, &term_orig); | 139 | tcsetattr(0, TCSANOW, &term_orig); |
| 191 | } | 140 | } |
| 192 | 141 | ||
| 193 | /* Set terminal input to raw mode */ | 142 | /* Set terminal input to raw mode */ |
| 194 | static void set_tty_raw() { | 143 | static void set_tty_raw(void) { |
| 195 | tcgetattr(0, &term_orig); | 144 | tcgetattr(0, &term_orig); |
| 196 | term_vi = term_orig; | 145 | term_vi = term_orig; |
| 197 | term_vi.c_lflag &= (~ICANON & ~ECHO); | 146 | term_vi.c_lflag &= (~ICANON & ~ECHO); |
| 198 | term_vi.c_iflag &= (~IXON & ~ICRNL); | 147 | term_vi.c_iflag &= (~IXON & ~ICRNL); |
| 199 | term_vi.c_oflag &= (~ONLCR); | 148 | term_vi.c_oflag &= (~ONLCR); |
| 200 | term_vi.c_cc[VMIN] = 1; | 149 | term_vi.c_cc[VMIN] = 1; |
| 201 | term_vi.c_cc[VTIME] = 0; | 150 | term_vi.c_cc[VTIME] = 0; |
| 202 | tcsetattr(0, TCSANOW, &term_vi); | 151 | tcsetattr(0, TCSANOW, &term_vi); |
| 203 | } | 152 | } |
| 204 | 153 | ||
| 205 | /* Exit the program gracefully */ | 154 | /* Exit the program gracefully */ |
| 206 | static void tless_exit(int code) { | 155 | static void tless_exit(int code) { |
| 207 | 156 | ||
| 208 | /* TODO: We really should save the terminal state when we start, | 157 | /* TODO: We really should save the terminal state when we start, |
| 209 | and restore it when we exit. Less does this with the | 158 | and restore it when we exit. Less does this with the |
| 210 | "ti" and "te" termcap commands; can this be done with | 159 | "ti" and "te" termcap commands; can this be done with |
| 211 | only termios.h? */ | 160 | only termios.h? */ |
| 212 | 161 | ||
| 213 | putchar('\n'); | 162 | putchar('\n'); |
| 214 | exit(code); | 163 | exit(code); |
| 215 | } | 164 | } |
| 216 | 165 | ||
| 217 | /* Grab a character from input without requiring the return key. If the | 166 | /* Grab a character from input without requiring the return key. If the |
| 218 | character is ASCII \033, get more characters and assign certain sequences | 167 | character is ASCII \033, get more characters and assign certain sequences |
| 219 | special return codes. Note that this function works best with raw input. */ | 168 | special return codes. Note that this function works best with raw input. */ |
| 220 | int tless_getch() { | 169 | static int tless_getch(void) { |
| 221 | 170 | ||
| 222 | set_tty_raw(); | 171 | set_tty_raw(); |
| 223 | char input_key[3]; | 172 | char input_key[3]; |
| 224 | 173 | ||
| 225 | input_key[0] = getc(inp); | 174 | input_key[0] = getc(inp); |
| 226 | /* Detect escape sequences (i.e. arrow keys) and handle | 175 | /* Detect escape sequences (i.e. arrow keys) and handle |
| 227 | them accordingly */ | 176 | them accordingly */ |
| 228 | 177 | ||
| 229 | if (input_key[0] == '\033') { | 178 | if (input_key[0] == '\033') { |
| 230 | input_key[1] = getc(inp); | 179 | input_key[1] = getc(inp); |
| 231 | input_key[2] = getc(inp); | 180 | input_key[2] = getc(inp); |
| @@ -253,25 +202,38 @@ int tless_getch() { | |||
| 253 | return 0; | 202 | return 0; |
| 254 | } | 203 | } |
| 255 | 204 | ||
| 256 | /* Move the cursor to a position (x,y), where (0,0) is the | 205 | /* Move the cursor to a position (x,y), where (0,0) is the |
| 257 | top-left corner of the console */ | 206 | top-left corner of the console */ |
| 258 | static void move_cursor(int x, int y) { | 207 | static void move_cursor(int x, int y) { |
| 259 | printf("\033[%i;%iH", x, y); | 208 | printf("\033[%i;%iH", x, y); |
| 260 | } | 209 | } |
| 261 | 210 | ||
| 262 | static void clear_line() { | 211 | static void clear_line(void) { |
| 263 | move_cursor(height, 0); | 212 | move_cursor(height, 0); |
| 264 | printf("\033[K"); | 213 | printf("\033[K"); |
| 265 | } | 214 | } |
| 266 | 215 | ||
| 267 | static void data_readlines() { | 216 | /* This adds line numbers to every line, as the -N flag necessitates */ |
| 268 | 217 | static void add_linenumbers(void) { | |
| 218 | |||
| 219 | char current_line[256]; | ||
| 220 | int i; | ||
| 221 | |||
| 222 | for (i = 0; i <= num_flines; i++) { | ||
| 223 | safe_strncpy(current_line, flines[i], 256); | ||
| 224 | flines[i] = xrealloc(flines[i], strlen(current_line) + 7 ); | ||
| 225 | sprintf(flines[i],"%5d %s", i+1, current_line); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | static void data_readlines(void) { | ||
| 230 | |||
| 269 | int i; | 231 | int i; |
| 270 | char current_line[256]; | 232 | char current_line[256]; |
| 271 | FILE *fp; | 233 | FILE *fp; |
| 272 | 234 | ||
| 273 | fp = (inp_stdin) ? stdin : bb_xfopen(filename, "rt"); | 235 | fp = (inp_stdin) ? stdin : bb_xfopen(filename, "rt"); |
| 274 | 236 | ||
| 275 | for (i = 0; (!feof(fp)) && (i <= MAXLINES); i++) { | 237 | for (i = 0; (!feof(fp)) && (i <= MAXLINES); i++) { |
| 276 | strcpy(current_line, ""); | 238 | strcpy(current_line, ""); |
| 277 | fgets(current_line, 256, fp); | 239 | fgets(current_line, 256, fp); |
| @@ -281,10 +243,10 @@ static void data_readlines() { | |||
| 281 | num_flines = i - 2; | 243 | num_flines = i - 2; |
| 282 | 244 | ||
| 283 | /* Reset variables for a new file */ | 245 | /* Reset variables for a new file */ |
| 284 | 246 | ||
| 285 | line_pos = 0; | 247 | line_pos = 0; |
| 286 | past_eof = 0; | 248 | past_eof = 0; |
| 287 | 249 | ||
| 288 | fclose(fp); | 250 | fclose(fp); |
| 289 | 251 | ||
| 290 | if (inp_stdin) | 252 | if (inp_stdin) |
| @@ -296,29 +258,29 @@ static void data_readlines() { | |||
| 296 | fclose(inp); | 258 | fclose(inp); |
| 297 | inp = fopen(CURRENT_TTY, "r"); | 259 | inp = fopen(CURRENT_TTY, "r"); |
| 298 | } | 260 | } |
| 299 | 261 | ||
| 300 | if (N_FLAG) | 262 | if (N_FLAG) |
| 301 | add_linenumbers(); | 263 | add_linenumbers(); |
| 302 | } | 264 | } |
| 303 | 265 | ||
| 304 | /* Free the file data */ | 266 | /* Free the file data */ |
| 305 | static void free_flines() { | 267 | static void free_flines(void) { |
| 306 | 268 | ||
| 307 | int i; | 269 | int i; |
| 308 | 270 | ||
| 309 | for (i = 0; i <= num_flines; i++) | 271 | for (i = 0; i <= num_flines; i++) |
| 310 | free(flines[i]); | 272 | free(flines[i]); |
| 311 | } | 273 | } |
| 312 | 274 | ||
| 313 | #ifdef CONFIG_FEATURE_LESS_FLAGS | 275 | #ifdef CONFIG_FEATURE_LESS_FLAGS |
| 314 | /* Calculate the percentage the current line position is through the file */ | 276 | /* Calculate the percentage the current line position is through the file */ |
| 315 | int calc_percent() { | 277 | static int calc_percent(void) { |
| 316 | return ((100 * (line_pos + height - 2) / num_flines) + 1); | 278 | return ((100 * (line_pos + height - 2) / num_flines) + 1); |
| 317 | } | 279 | } |
| 318 | #endif | 280 | #endif |
| 319 | 281 | ||
| 320 | /* Turn a percentage into a line number */ | 282 | /* Turn a percentage into a line number */ |
| 321 | int reverse_percent(int percentage) { | 283 | static int reverse_percent(int percentage) { |
| 322 | double linenum = percentage; | 284 | double linenum = percentage; |
| 323 | linenum = ((linenum / 100) * num_flines) - 1; | 285 | linenum = ((linenum / 100) * num_flines) - 1; |
| 324 | return(linenum); | 286 | return(linenum); |
| @@ -326,10 +288,10 @@ int reverse_percent(int percentage) { | |||
| 326 | 288 | ||
| 327 | #ifdef CONFIG_FEATURE_LESS_FLAGS | 289 | #ifdef CONFIG_FEATURE_LESS_FLAGS |
| 328 | /* Print a status line if -M was specified */ | 290 | /* Print a status line if -M was specified */ |
| 329 | static void m_status_print() { | 291 | static void m_status_print(void) { |
| 330 | 292 | ||
| 331 | int percentage; | 293 | int percentage; |
| 332 | 294 | ||
| 333 | if (!past_eof) { | 295 | if (!past_eof) { |
| 334 | if (!line_pos) { | 296 | if (!line_pos) { |
| 335 | if (num_files > 1) | 297 | if (num_files > 1) |
| @@ -341,7 +303,7 @@ static void m_status_print() { | |||
| 341 | else { | 303 | else { |
| 342 | printf("%s %s lines %i-%i/%i ", HIGHLIGHT, filename, line_pos + 1, line_pos + height - 1, num_flines + 1); | 304 | printf("%s %s lines %i-%i/%i ", HIGHLIGHT, filename, line_pos + 1, line_pos + height - 1, num_flines + 1); |
| 343 | } | 305 | } |
| 344 | 306 | ||
| 345 | if (line_pos == num_flines - height + 2) { | 307 | if (line_pos == num_flines - height + 2) { |
| 346 | printf("(END) %s", NORMAL); | 308 | printf("(END) %s", NORMAL); |
| 347 | if ((num_files > 1) && (current_file != num_files)) | 309 | if ((num_files > 1) && (current_file != num_files)) |
| @@ -361,11 +323,11 @@ static void m_status_print() { | |||
| 361 | } | 323 | } |
| 362 | 324 | ||
| 363 | /* Print a status line if -m was specified */ | 325 | /* Print a status line if -m was specified */ |
| 364 | static void medium_status_print() { | 326 | static void medium_status_print(void) { |
| 365 | 327 | ||
| 366 | int percentage; | 328 | int percentage; |
| 367 | percentage = calc_percent(); | 329 | percentage = calc_percent(); |
| 368 | 330 | ||
| 369 | if (!line_pos) | 331 | if (!line_pos) |
| 370 | printf("%s%s %i%s%s", HIGHLIGHT, filename, percentage, "%", NORMAL); | 332 | printf("%s%s %i%s%s", HIGHLIGHT, filename, percentage, "%", NORMAL); |
| 371 | else if (line_pos == num_flines - height + 2) | 333 | else if (line_pos == num_flines - height + 2) |
| @@ -376,11 +338,11 @@ static void medium_status_print() { | |||
| 376 | #endif | 338 | #endif |
| 377 | 339 | ||
| 378 | /* Print the status line */ | 340 | /* Print the status line */ |
| 379 | static void status_print() { | 341 | static void status_print(void) { |
| 380 | 342 | ||
| 381 | /* Change the status if flags have been set */ | 343 | /* Change the status if flags have been set */ |
| 382 | #ifdef CONFIG_FEATURE_LESS_FLAGS | 344 | #ifdef CONFIG_FEATURE_LESS_FLAGS |
| 383 | if (M_FLAG) | 345 | if (M_FLAG) |
| 384 | m_status_print(); | 346 | m_status_print(); |
| 385 | else if (m_FLAG) | 347 | else if (m_FLAG) |
| 386 | medium_status_print(); | 348 | medium_status_print(); |
| @@ -406,10 +368,10 @@ static void status_print() { | |||
| 406 | } | 368 | } |
| 407 | 369 | ||
| 408 | /* Print the buffer */ | 370 | /* Print the buffer */ |
| 409 | static void buffer_print() { | 371 | static void buffer_print(void) { |
| 410 | 372 | ||
| 411 | int i; | 373 | int i; |
| 412 | 374 | ||
| 413 | if (num_flines >= height - 2) { | 375 | if (num_flines >= height - 2) { |
| 414 | printf("%s", CLEAR); | 376 | printf("%s", CLEAR); |
| 415 | move_cursor(0,0); | 377 | move_cursor(0,0); |
| @@ -429,19 +391,19 @@ static void buffer_print() { | |||
| 429 | } | 391 | } |
| 430 | 392 | ||
| 431 | /* Initialise the buffer */ | 393 | /* Initialise the buffer */ |
| 432 | static void buffer_init() { | 394 | static void buffer_init(void) { |
| 433 | 395 | ||
| 434 | int i; | 396 | int i; |
| 435 | 397 | ||
| 436 | for (i = 0; i < (height - 1); i++) | 398 | for (i = 0; i < (height - 1); i++) |
| 437 | memset(buffer[i], '\0', 256); | 399 | memset(buffer[i], '\0', 256); |
| 438 | 400 | ||
| 439 | /* Fill the buffer until the end of the file or the | 401 | /* Fill the buffer until the end of the file or the |
| 440 | end of the buffer is reached */ | 402 | end of the buffer is reached */ |
| 441 | for (i = 0; (i < (height - 1)) && (i <= num_flines); i++) { | 403 | for (i = 0; (i < (height - 1)) && (i <= num_flines); i++) { |
| 442 | strcpy(buffer[i], flines[i]); | 404 | strcpy(buffer[i], flines[i]); |
| 443 | } | 405 | } |
| 444 | 406 | ||
| 445 | /* If the buffer still isn't full, fill it with blank lines */ | 407 | /* If the buffer still isn't full, fill it with blank lines */ |
| 446 | for (; i < (height - 1); i++) { | 408 | for (; i < (height - 1); i++) { |
| 447 | strcpy(buffer[i], ""); | 409 | strcpy(buffer[i], ""); |
| @@ -450,9 +412,9 @@ static void buffer_init() { | |||
| 450 | 412 | ||
| 451 | /* Move the buffer up and down in the file in order to scroll */ | 413 | /* Move the buffer up and down in the file in order to scroll */ |
| 452 | static void buffer_down(int nlines) { | 414 | static void buffer_down(int nlines) { |
| 453 | 415 | ||
| 454 | int i; | 416 | int i; |
| 455 | 417 | ||
| 456 | if (!past_eof) { | 418 | if (!past_eof) { |
| 457 | if (line_pos + (height - 3) + nlines < num_flines) { | 419 | if (line_pos + (height - 3) + nlines < num_flines) { |
| 458 | line_pos += nlines; | 420 | line_pos += nlines; |
| @@ -461,8 +423,8 @@ static void buffer_down(int nlines) { | |||
| 461 | } | 423 | } |
| 462 | else { | 424 | else { |
| 463 | /* As the number of lines requested was too large, we just move | 425 | /* As the number of lines requested was too large, we just move |
| 464 | to the end of the file */ | 426 | to the end of the file */ |
| 465 | while (line_pos + (height - 3) + 1 < num_flines) { | 427 | while (line_pos + (height - 3) + 1 < num_flines) { |
| 466 | line_pos += 1; | 428 | line_pos += 1; |
| 467 | for (i = 0; i < (height - 1); i++) | 429 | for (i = 0; i < (height - 1); i++) |
| 468 | strcpy(buffer[i], flines[line_pos + i]); | 430 | strcpy(buffer[i], flines[line_pos + i]); |
| @@ -476,10 +438,10 @@ static void buffer_down(int nlines) { | |||
| 476 | } | 438 | } |
| 477 | 439 | ||
| 478 | static void buffer_up(int nlines) { | 440 | static void buffer_up(int nlines) { |
| 479 | 441 | ||
| 480 | int i; | 442 | int i; |
| 481 | int tilde_line; | 443 | int tilde_line; |
| 482 | 444 | ||
| 483 | if (!past_eof) { | 445 | if (!past_eof) { |
| 484 | if (line_pos - nlines >= 0) { | 446 | if (line_pos - nlines >= 0) { |
| 485 | line_pos -= nlines; | 447 | line_pos -= nlines; |
| @@ -499,7 +461,7 @@ static void buffer_up(int nlines) { | |||
| 499 | else { | 461 | else { |
| 500 | /* Work out where the tildes start */ | 462 | /* Work out where the tildes start */ |
| 501 | tilde_line = num_flines - line_pos + 3; | 463 | tilde_line = num_flines - line_pos + 3; |
| 502 | 464 | ||
| 503 | line_pos -= nlines; | 465 | line_pos -= nlines; |
| 504 | /* Going backwards nlines lines has taken us to a point where | 466 | /* Going backwards nlines lines has taken us to a point where |
| 505 | nothing is past the EOF, so we revert to normal. */ | 467 | nothing is past the EOF, so we revert to normal. */ |
| @@ -509,7 +471,7 @@ static void buffer_up(int nlines) { | |||
| 509 | } | 471 | } |
| 510 | else { | 472 | else { |
| 511 | /* We only move part of the buffer, as the rest | 473 | /* We only move part of the buffer, as the rest |
| 512 | is past the EOF */ | 474 | is past the EOF */ |
| 513 | for (i = 0; i < (height - 1); i++) { | 475 | for (i = 0; i < (height - 1); i++) { |
| 514 | if (i < tilde_line - nlines + 1) | 476 | if (i < tilde_line - nlines + 1) |
| 515 | strcpy(buffer[i], flines[line_pos + i]); | 477 | strcpy(buffer[i], flines[line_pos + i]); |
| @@ -518,12 +480,12 @@ static void buffer_up(int nlines) { | |||
| 518 | strcpy(buffer[i], "~\n"); | 480 | strcpy(buffer[i], "~\n"); |
| 519 | } | 481 | } |
| 520 | } | 482 | } |
| 521 | } | 483 | } |
| 522 | } | 484 | } |
| 523 | } | 485 | } |
| 524 | 486 | ||
| 525 | static void buffer_line(int linenum) { | 487 | static void buffer_line(int linenum) { |
| 526 | 488 | ||
| 527 | int i; | 489 | int i; |
| 528 | 490 | ||
| 529 | past_eof = 0; | 491 | past_eof = 0; |
| @@ -550,125 +512,103 @@ static void buffer_line(int linenum) { | |||
| 550 | } | 512 | } |
| 551 | } | 513 | } |
| 552 | 514 | ||
| 553 | static void keypress_process(int keypress) { | 515 | static void examine_file(void) { |
| 554 | switch (keypress) { | 516 | |
| 555 | case KEY_DOWN: case 'e': case 'j': case '\015': | 517 | int newline_offset; |
| 556 | buffer_down(1); | 518 | |
| 557 | buffer_print(); | 519 | clear_line(); |
| 558 | break; | 520 | printf("Examine: "); |
| 559 | case KEY_UP: case 'y': case 'k': | 521 | fgets(filename, 256, inp); |
| 560 | buffer_up(1); | 522 | |
| 561 | buffer_print(); | 523 | /* As fgets adds a newline to the end of an input string, we |
| 562 | break; | 524 | need to remove it */ |
| 563 | case PAGE_DOWN: case ' ': case 'z': | 525 | newline_offset = strlen(filename) - 1; |
| 564 | buffer_down(height - 1); | 526 | filename[newline_offset] = '\0'; |
| 565 | buffer_print(); | 527 | |
| 566 | break; | 528 | files[num_files] = bb_xstrndup(filename, (strlen(filename) + 1) * sizeof(char)); |
| 567 | case PAGE_UP: case 'w': case 'b': | 529 | current_file = num_files + 1; |
| 568 | buffer_up(height - 1); | 530 | num_files++; |
| 569 | buffer_print(); | 531 | |
| 570 | break; | 532 | inp_stdin = 0; |
| 571 | case 'd': | 533 | ea_inp_stdin = 1; |
| 572 | buffer_down((height - 1) / 2); | 534 | free_flines(); |
| 573 | buffer_print(); | 535 | data_readlines(); |
| 574 | break; | 536 | buffer_init(); |
| 575 | case 'u': | 537 | buffer_print(); |
| 576 | buffer_up((height - 1) / 2); | 538 | } |
| 577 | buffer_print(); | 539 | |
| 578 | break; | 540 | |
| 579 | case 'g': case 'p': case '<': case '%': | 541 | static void next_file(void) { |
| 580 | buffer_up(num_flines + 1); | 542 | if (current_file != num_files) { |
| 581 | buffer_print(); | 543 | current_file++; |
| 582 | break; | 544 | strcpy(filename, files[current_file - 1]); |
| 583 | case 'G': case '>': | 545 | free_flines(); |
| 584 | buffer_down(num_flines + 1); | 546 | data_readlines(); |
| 585 | buffer_print(); | 547 | buffer_init(); |
| 586 | break; | 548 | buffer_print(); |
| 587 | case 'q': case 'Q': | 549 | } |
| 588 | tless_exit(0); | 550 | else { |
| 589 | break; | 551 | clear_line(); |
| 590 | #ifdef CONFIG_FEATURE_LESS_MARKS | 552 | printf("%s%s%s", HIGHLIGHT, "No next file", NORMAL); |
| 591 | case 'm': | 553 | } |
| 592 | add_mark(); | 554 | } |
| 593 | buffer_print(); | 555 | |
| 594 | break; | 556 | static void previous_file(void) { |
| 595 | case '\'': | 557 | if (current_file != 1) { |
| 596 | goto_mark(); | 558 | current_file--; |
| 597 | buffer_print(); | 559 | strcpy(filename, files[current_file - 1]); |
| 598 | break; | 560 | |
| 599 | #endif | 561 | free_flines(); |
| 600 | case 'r': | 562 | data_readlines(); |
| 601 | buffer_print(); | 563 | buffer_init(); |
| 602 | break; | 564 | buffer_print(); |
| 603 | case 'R': | 565 | } |
| 604 | full_repaint(); | 566 | else { |
| 605 | break; | 567 | clear_line(); |
| 606 | case 's': | 568 | printf("%s%s%s", HIGHLIGHT, "No previous file", NORMAL); |
| 607 | if (inp_stdin) | ||
| 608 | save_input_to_file(); | ||
| 609 | break; | ||
| 610 | case 'E': | ||
| 611 | examine_file(); | ||
| 612 | break; | ||
| 613 | #ifdef CONFIG_FEATURE_LESS_FLAGS | ||
| 614 | case '=': | ||
| 615 | clear_line(); | ||
| 616 | m_status_print(); | ||
| 617 | break; | ||
| 618 | #endif | ||
| 619 | #ifdef CONFIG_FEATURE_LESS_REGEXP | ||
| 620 | case '/': | ||
| 621 | regex_process(); | ||
| 622 | buffer_print(); | ||
| 623 | break; | ||
| 624 | case 'n': | ||
| 625 | goto_match(match_pos + 1); | ||
| 626 | buffer_print(); | ||
| 627 | break; | ||
| 628 | case 'N': | ||
| 629 | goto_match(match_pos - 1); | ||
| 630 | buffer_print(); | ||
| 631 | break; | ||
| 632 | case '?': | ||
| 633 | search_backwards(); | ||
| 634 | buffer_print(); | ||
| 635 | break; | ||
| 636 | #endif | ||
| 637 | #ifdef CONFIG_FEATURE_LESS_FLAGCS | ||
| 638 | case '-': | ||
| 639 | flag_change(); | ||
| 640 | buffer_print(); | ||
| 641 | break; | ||
| 642 | case '_': | ||
| 643 | show_flag_status(); | ||
| 644 | break; | ||
| 645 | #endif | ||
| 646 | #ifdef CONFIG_FEATURE_LESS_BRACKETS | ||
| 647 | case '{': case '(': case '[': | ||
| 648 | match_right_bracket(keypress); | ||
| 649 | break; | ||
| 650 | case '}': case ')': case ']': | ||
| 651 | match_left_bracket(keypress); | ||
| 652 | break; | ||
| 653 | #endif | ||
| 654 | case ':': | ||
| 655 | colon_process(); | ||
| 656 | break; | ||
| 657 | default: | ||
| 658 | break; | ||
| 659 | } | 569 | } |
| 660 | if (isdigit(keypress)) | ||
| 661 | number_process(keypress); | ||
| 662 | } | 570 | } |
| 663 | 571 | ||
| 664 | static void colon_process() { | 572 | static void first_file(void) { |
| 665 | 573 | if (current_file != 1) { | |
| 574 | current_file = 1; | ||
| 575 | strcpy(filename, files[current_file - 1]); | ||
| 576 | free_flines(); | ||
| 577 | data_readlines(); | ||
| 578 | buffer_init(); | ||
| 579 | buffer_print(); | ||
| 580 | } | ||
| 581 | } | ||
| 582 | |||
| 583 | static void remove_current_file(void) { | ||
| 584 | |||
| 585 | int i; | ||
| 586 | |||
| 587 | if (current_file != 1) { | ||
| 588 | previous_file(); | ||
| 589 | for (i = 3; i <= num_files; i++) | ||
| 590 | files[i - 2] = files[i - 1]; | ||
| 591 | num_files--; | ||
| 592 | buffer_print(); | ||
| 593 | } | ||
| 594 | else { | ||
| 595 | next_file(); | ||
| 596 | for (i = 2; i <= num_files; i++) | ||
| 597 | files[i - 2] = files[i - 1]; | ||
| 598 | num_files--; | ||
| 599 | current_file--; | ||
| 600 | buffer_print(); | ||
| 601 | } | ||
| 602 | } | ||
| 603 | |||
| 604 | static void colon_process(void) { | ||
| 605 | |||
| 666 | int keypress; | 606 | int keypress; |
| 667 | 607 | ||
| 668 | /* Clear the current line and print a prompt */ | 608 | /* Clear the current line and print a prompt */ |
| 669 | clear_line(); | 609 | clear_line(); |
| 670 | printf(" :"); | 610 | printf(" :"); |
| 671 | 611 | ||
| 672 | keypress = tless_getch(); | 612 | keypress = tless_getch(); |
| 673 | switch (keypress) { | 613 | switch (keypress) { |
| 674 | case 'd': | 614 | case 'd': |
| @@ -700,24 +640,158 @@ static void colon_process() { | |||
| 700 | } | 640 | } |
| 701 | } | 641 | } |
| 702 | 642 | ||
| 643 | #ifdef CONFIG_FEATURE_LESS_REGEXP | ||
| 644 | /* The below two regular expression handler functions NEED development. */ | ||
| 645 | |||
| 646 | /* Get a regular expression from the user, and then go through the current | ||
| 647 | file line by line, running a processing regex function on each one. */ | ||
| 648 | |||
| 649 | static char *insert_highlights (char *line, int start, int end) { | ||
| 650 | |||
| 651 | char *new_line = (char *) malloc((sizeof(char) * (strlen(line) + 1)) + 10); | ||
| 652 | |||
| 653 | memset(new_line, 0, ((sizeof(char) * (strlen(line) + 1)) + 10)); | ||
| 654 | strncat(new_line, line, start); | ||
| 655 | strcat(new_line, HIGHLIGHT); | ||
| 656 | strncat(new_line, line + start, end - start); | ||
| 657 | strcat(new_line, NORMAL); | ||
| 658 | strncat(new_line, line + end, strlen(line) - end); | ||
| 659 | |||
| 660 | return new_line; | ||
| 661 | } | ||
| 662 | |||
| 663 | static char *process_regex_on_line(char *line, regex_t *pattern) { | ||
| 664 | /* This function takes the regex and applies it to the line. | ||
| 665 | Each part of the line that matches has the HIGHLIGHT | ||
| 666 | and NORMAL escape sequences placed around it by | ||
| 667 | insert_highlights, and then the line is returned. */ | ||
| 668 | |||
| 669 | int match_status; | ||
| 670 | char *line2 = (char *) malloc((sizeof(char) * (strlen(line) + 1)) + 64); | ||
| 671 | char sub_line[256]; | ||
| 672 | int prev_eo = 0; | ||
| 673 | memset(sub_line, 0, 256); | ||
| 674 | strcpy(line2, line); | ||
| 675 | regmatch_t match_structs; | ||
| 676 | |||
| 677 | match_found = 0; | ||
| 678 | match_status = regexec(pattern, line2, 1, &match_structs, 0); | ||
| 679 | |||
| 680 | while (match_status == 0) { | ||
| 681 | |||
| 682 | memset(sub_line, 0, 256); | ||
| 683 | |||
| 684 | if (match_found == 0) | ||
| 685 | match_found = 1; | ||
| 686 | |||
| 687 | line2 = insert_highlights(line2, match_structs.rm_so + prev_eo, match_structs.rm_eo + prev_eo); | ||
| 688 | if (match_structs.rm_eo + 11 + prev_eo < strlen(line2)) | ||
| 689 | strcat(sub_line, line2 + match_structs.rm_eo + 11 + prev_eo); | ||
| 690 | |||
| 691 | prev_eo += match_structs.rm_eo + 11; | ||
| 692 | match_status = regexec(pattern, sub_line, 1, &match_structs, REG_NOTBOL); | ||
| 693 | } | ||
| 694 | |||
| 695 | return line2; | ||
| 696 | } | ||
| 697 | |||
| 698 | static void regex_process(void) { | ||
| 699 | |||
| 700 | char uncomp_regex[100]; | ||
| 701 | char current_line[256]; | ||
| 702 | int i; | ||
| 703 | int j = 0; | ||
| 704 | regex_t *pattern; | ||
| 705 | |||
| 706 | /* Reset variables */ | ||
| 707 | match_lines[0] = -1; | ||
| 708 | match_pos = 0; | ||
| 709 | num_matches = 0; | ||
| 710 | match_found = 0; | ||
| 711 | |||
| 712 | pattern = (regex_t *) malloc(sizeof(regex_t)); | ||
| 713 | memset(pattern, 0, sizeof(regex_t)); | ||
| 714 | |||
| 715 | /* Get the uncompiled regular expression from the user */ | ||
| 716 | clear_line(); | ||
| 717 | if (match_backwards) | ||
| 718 | printf("?"); | ||
| 719 | else | ||
| 720 | printf("/"); | ||
| 721 | scanf("%s", uncomp_regex); | ||
| 722 | |||
| 723 | /* Compile the regex and check for errors */ | ||
| 724 | xregcomp(pattern, uncomp_regex, 0); | ||
| 725 | |||
| 726 | /* Run the regex on each line of the current file here */ | ||
| 727 | for (i = 0; i <= num_flines; i++) { | ||
| 728 | strcpy(current_line, process_regex_on_line(flines[i], pattern)); | ||
| 729 | flines[i] = (char *) bb_xstrndup(current_line, sizeof(char) * (strlen(current_line)+1)); | ||
| 730 | |||
| 731 | if (match_found) { | ||
| 732 | match_lines[j] = i; | ||
| 733 | j++; | ||
| 734 | } | ||
| 735 | } | ||
| 736 | |||
| 737 | num_matches = j; | ||
| 738 | |||
| 739 | if ((match_lines[0] != -1) && (num_flines > height - 2)) | ||
| 740 | buffer_line(match_lines[0]); | ||
| 741 | else | ||
| 742 | buffer_init(); | ||
| 743 | } | ||
| 744 | |||
| 745 | static void goto_match(int match) { | ||
| 746 | |||
| 747 | /* This goes to a specific match - all line positions of matches are | ||
| 748 | stored within the match_lines[] array. */ | ||
| 749 | if ((match < num_matches) && (match >= 0)) { | ||
| 750 | buffer_line(match_lines[match]); | ||
| 751 | match_pos = match; | ||
| 752 | } | ||
| 753 | } | ||
| 754 | |||
| 755 | static void search_backwards(void) { | ||
| 756 | |||
| 757 | int current_linepos = line_pos; | ||
| 758 | int i; | ||
| 759 | |||
| 760 | match_backwards = 1; | ||
| 761 | regex_process(); | ||
| 762 | |||
| 763 | for (i = 0; i < num_matches; i++) { | ||
| 764 | if (match_lines[i] > current_linepos) { | ||
| 765 | buffer_line(match_lines[i - num_back_match]); | ||
| 766 | break; | ||
| 767 | } | ||
| 768 | } | ||
| 769 | |||
| 770 | /* Reset variables */ | ||
| 771 | match_backwards = 0; | ||
| 772 | num_back_match = 1; | ||
| 773 | |||
| 774 | } | ||
| 775 | #endif | ||
| 776 | |||
| 703 | static void number_process(int first_digit) { | 777 | static void number_process(int first_digit) { |
| 704 | 778 | ||
| 705 | int i = 1; | 779 | int i = 1; |
| 706 | int num; | 780 | int num; |
| 707 | char num_input[80]; | 781 | char num_input[80]; |
| 708 | char keypress; | 782 | char keypress; |
| 709 | num_input[0] = first_digit; | 783 | num_input[0] = first_digit; |
| 710 | 784 | ||
| 711 | /* Clear the current line, print a prompt, and then print the digit */ | 785 | /* Clear the current line, print a prompt, and then print the digit */ |
| 712 | clear_line(); | 786 | clear_line(); |
| 713 | printf(":%c", first_digit); | 787 | printf(":%c", first_digit); |
| 714 | 788 | ||
| 715 | /* Receive input until a letter is given */ | 789 | /* Receive input until a letter is given */ |
| 716 | while((num_input[i] = tless_getch()) && isdigit(num_input[i])) { | 790 | while((num_input[i] = tless_getch()) && isdigit(num_input[i])) { |
| 717 | printf("%c",num_input[i]); | 791 | printf("%c",num_input[i]); |
| 718 | i++; | 792 | i++; |
| 719 | } | 793 | } |
| 720 | 794 | ||
| 721 | /* Take the final letter out of the digits string */ | 795 | /* Take the final letter out of the digits string */ |
| 722 | keypress = num_input[i]; | 796 | keypress = num_input[i]; |
| 723 | num_input[i] = '\0'; | 797 | num_input[i] = '\0'; |
| @@ -765,14 +839,14 @@ static void number_process(int first_digit) { | |||
| 765 | } | 839 | } |
| 766 | 840 | ||
| 767 | #ifdef CONFIG_FEATURE_LESS_FLAGCS | 841 | #ifdef CONFIG_FEATURE_LESS_FLAGCS |
| 768 | static void flag_change() { | 842 | static void flag_change(void) { |
| 769 | 843 | ||
| 770 | int keypress; | 844 | int keypress; |
| 771 | 845 | ||
| 772 | clear_line(); | 846 | clear_line(); |
| 773 | printf("-"); | 847 | printf("-"); |
| 774 | keypress = tless_getch(); | 848 | keypress = tless_getch(); |
| 775 | 849 | ||
| 776 | switch (keypress) { | 850 | switch (keypress) { |
| 777 | case 'M': | 851 | case 'M': |
| 778 | M_FLAG = !M_FLAG; | 852 | M_FLAG = !M_FLAG; |
| @@ -791,11 +865,11 @@ static void flag_change() { | |||
| 791 | } | 865 | } |
| 792 | } | 866 | } |
| 793 | 867 | ||
| 794 | static void show_flag_status() { | 868 | static void show_flag_status(void) { |
| 795 | 869 | ||
| 796 | int keypress; | 870 | int keypress; |
| 797 | int flag_val; | 871 | int flag_val; |
| 798 | 872 | ||
| 799 | clear_line(); | 873 | clear_line(); |
| 800 | printf("_"); | 874 | printf("_"); |
| 801 | keypress = tless_getch(); | 875 | keypress = tless_getch(); |
| @@ -820,101 +894,13 @@ static void show_flag_status() { | |||
| 820 | flag_val = 0; | 894 | flag_val = 0; |
| 821 | break; | 895 | break; |
| 822 | } | 896 | } |
| 823 | |||
| 824 | clear_line(); | ||
| 825 | printf("%s%s%i%s", HIGHLIGHT, "The status of the flag is: ", flag_val, NORMAL); | ||
| 826 | } | ||
| 827 | #endif | ||
| 828 | 897 | ||
| 829 | static void examine_file() { | ||
| 830 | |||
| 831 | int newline_offset; | ||
| 832 | |||
| 833 | clear_line(); | 898 | clear_line(); |
| 834 | printf("Examine: "); | 899 | printf("%s%s%i%s", HIGHLIGHT, "The status of the flag is: ", flag_val != 0, NORMAL); |
| 835 | fgets(filename, 256, inp); | ||
| 836 | |||
| 837 | /* As fgets adds a newline to the end of an input string, we | ||
| 838 | need to remove it */ | ||
| 839 | newline_offset = strlen(filename) - 1; | ||
| 840 | filename[newline_offset] = '\0'; | ||
| 841 | |||
| 842 | files[num_files] = bb_xstrndup(filename, (strlen(filename) + 1) * sizeof(char)); | ||
| 843 | current_file = num_files + 1; | ||
| 844 | num_files++; | ||
| 845 | |||
| 846 | inp_stdin = 0; | ||
| 847 | ea_inp_stdin = 1; | ||
| 848 | free_flines(); | ||
| 849 | data_readlines(); | ||
| 850 | buffer_init(); | ||
| 851 | buffer_print(); | ||
| 852 | } | ||
| 853 | |||
| 854 | static void next_file() { | ||
| 855 | if (current_file != num_files) { | ||
| 856 | current_file++; | ||
| 857 | strcpy(filename, files[current_file - 1]); | ||
| 858 | free_flines(); | ||
| 859 | data_readlines(); | ||
| 860 | buffer_init(); | ||
| 861 | buffer_print(); | ||
| 862 | } | ||
| 863 | else { | ||
| 864 | clear_line(); | ||
| 865 | printf("%s%s%s", HIGHLIGHT, "No next file", NORMAL); | ||
| 866 | } | ||
| 867 | } | ||
| 868 | |||
| 869 | static void previous_file() { | ||
| 870 | if (current_file != 1) { | ||
| 871 | current_file--; | ||
| 872 | strcpy(filename, files[current_file - 1]); | ||
| 873 | |||
| 874 | free_flines(); | ||
| 875 | data_readlines(); | ||
| 876 | buffer_init(); | ||
| 877 | buffer_print(); | ||
| 878 | } | ||
| 879 | else { | ||
| 880 | clear_line(); | ||
| 881 | printf("%s%s%s", HIGHLIGHT, "No previous file", NORMAL); | ||
| 882 | } | ||
| 883 | } | ||
| 884 | |||
| 885 | static void first_file() { | ||
| 886 | if (current_file != 1) { | ||
| 887 | current_file = 1; | ||
| 888 | strcpy(filename, files[current_file - 1]); | ||
| 889 | free_flines(); | ||
| 890 | data_readlines(); | ||
| 891 | buffer_init(); | ||
| 892 | buffer_print(); | ||
| 893 | } | ||
| 894 | } | ||
| 895 | |||
| 896 | static void remove_current_file() { | ||
| 897 | |||
| 898 | int i; | ||
| 899 | |||
| 900 | if (current_file != 1) { | ||
| 901 | previous_file(); | ||
| 902 | for (i = 3; i <= num_files; i++) | ||
| 903 | files[i - 2] = files[i - 1]; | ||
| 904 | num_files--; | ||
| 905 | buffer_print(); | ||
| 906 | } | ||
| 907 | else { | ||
| 908 | next_file(); | ||
| 909 | for (i = 2; i <= num_files; i++) | ||
| 910 | files[i - 2] = files[i - 1]; | ||
| 911 | num_files--; | ||
| 912 | current_file--; | ||
| 913 | buffer_print(); | ||
| 914 | } | ||
| 915 | } | 900 | } |
| 901 | #endif | ||
| 916 | 902 | ||
| 917 | static void full_repaint() { | 903 | static void full_repaint(void) { |
| 918 | 904 | ||
| 919 | int temp_line_pos = line_pos; | 905 | int temp_line_pos = line_pos; |
| 920 | data_readlines(); | 906 | data_readlines(); |
| @@ -923,25 +909,13 @@ static void full_repaint() { | |||
| 923 | buffer_print(); | 909 | buffer_print(); |
| 924 | } | 910 | } |
| 925 | 911 | ||
| 926 | /* This adds line numbers to every line, as the -N flag necessitates */ | ||
| 927 | static void add_linenumbers() { | ||
| 928 | 912 | ||
| 929 | char current_line[256]; | 913 | static void save_input_to_file(void) { |
| 930 | int i; | ||
| 931 | |||
| 932 | for (i = 0; i <= num_flines; i++) { | ||
| 933 | safe_strncpy(current_line, flines[i], 256); | ||
| 934 | flines[i] = xrealloc(flines[i], strlen(current_line) + 7 ); | ||
| 935 | sprintf(flines[i],"%5d %s", i+1, current_line); | ||
| 936 | } | ||
| 937 | } | ||
| 938 | 914 | ||
| 939 | static void save_input_to_file() { | ||
| 940 | |||
| 941 | char current_line[256]; | 915 | char current_line[256]; |
| 942 | int i; | 916 | int i; |
| 943 | FILE *fp; | 917 | FILE *fp; |
| 944 | 918 | ||
| 945 | clear_line(); | 919 | clear_line(); |
| 946 | printf("Log file: "); | 920 | printf("Log file: "); |
| 947 | fgets(current_line, 256, inp); | 921 | fgets(current_line, 256, inp); |
| @@ -958,18 +932,18 @@ static void save_input_to_file() { | |||
| 958 | } | 932 | } |
| 959 | 933 | ||
| 960 | #ifdef CONFIG_FEATURE_LESS_MARKS | 934 | #ifdef CONFIG_FEATURE_LESS_MARKS |
| 961 | static void add_mark() { | 935 | static void add_mark(void) { |
| 962 | 936 | ||
| 963 | int letter; | 937 | int letter; |
| 964 | int mark_line; | 938 | int mark_line; |
| 965 | 939 | ||
| 966 | clear_line(); | 940 | clear_line(); |
| 967 | printf("Mark: "); | 941 | printf("Mark: "); |
| 968 | letter = tless_getch(); | 942 | letter = tless_getch(); |
| 969 | 943 | ||
| 970 | if (isalpha(letter)) { | 944 | if (isalpha(letter)) { |
| 971 | mark_line = line_pos; | 945 | mark_line = line_pos; |
| 972 | 946 | ||
| 973 | /* If we exceed 15 marks, start overwriting previous ones */ | 947 | /* If we exceed 15 marks, start overwriting previous ones */ |
| 974 | if (num_marks == 14) | 948 | if (num_marks == 14) |
| 975 | num_marks = 0; | 949 | num_marks = 0; |
| @@ -984,11 +958,11 @@ static void add_mark() { | |||
| 984 | } | 958 | } |
| 985 | } | 959 | } |
| 986 | 960 | ||
| 987 | static void goto_mark() { | 961 | static void goto_mark(void) { |
| 988 | 962 | ||
| 989 | int letter; | 963 | int letter; |
| 990 | int i; | 964 | int i; |
| 991 | 965 | ||
| 992 | clear_line(); | 966 | clear_line(); |
| 993 | printf("Go to mark: "); | 967 | printf("Go to mark: "); |
| 994 | letter = tless_getch(); | 968 | letter = tless_getch(); |
| @@ -1001,7 +975,7 @@ static void goto_mark() { | |||
| 1001 | if ((num_marks == 14) && (letter != mark_lines[14][0])) { | 975 | if ((num_marks == 14) && (letter != mark_lines[14][0])) { |
| 1002 | clear_line(); | 976 | clear_line(); |
| 1003 | printf("%s%s%s", HIGHLIGHT, "Mark not set", NORMAL); | 977 | printf("%s%s%s", HIGHLIGHT, "Mark not set", NORMAL); |
| 1004 | } | 978 | } |
| 1005 | } | 979 | } |
| 1006 | else { | 980 | else { |
| 1007 | clear_line(); | 981 | clear_line(); |
| @@ -1010,137 +984,6 @@ static void goto_mark() { | |||
| 1010 | } | 984 | } |
| 1011 | #endif | 985 | #endif |
| 1012 | 986 | ||
| 1013 | #ifdef CONFIG_FEATURE_LESS_REGEXP | ||
| 1014 | /* The below two regular expression handler functions NEED development. */ | ||
| 1015 | |||
| 1016 | /* Get a regular expression from the user, and then go through the current | ||
| 1017 | file line by line, running a processing regex function on each one. */ | ||
| 1018 | static void regex_process() { | ||
| 1019 | |||
| 1020 | char uncomp_regex[100]; | ||
| 1021 | char current_line[256]; | ||
| 1022 | int i; | ||
| 1023 | int j = 0; | ||
| 1024 | regex_t *pattern; | ||
| 1025 | |||
| 1026 | /* Reset variables */ | ||
| 1027 | match_lines[0] = -1; | ||
| 1028 | match_pos = 0; | ||
| 1029 | num_matches = 0; | ||
| 1030 | match_found = 0; | ||
| 1031 | |||
| 1032 | pattern = (regex_t *) malloc(sizeof(regex_t)); | ||
| 1033 | memset(pattern, 0, sizeof(regex_t)); | ||
| 1034 | |||
| 1035 | /* Get the uncompiled regular expression from the user */ | ||
| 1036 | clear_line(); | ||
| 1037 | if (match_backwards) | ||
| 1038 | printf("?"); | ||
| 1039 | else | ||
| 1040 | printf("/"); | ||
| 1041 | scanf("%s", uncomp_regex); | ||
| 1042 | |||
| 1043 | /* Compile the regex and check for errors */ | ||
| 1044 | xregcomp(pattern, uncomp_regex, 0); | ||
| 1045 | |||
| 1046 | /* Run the regex on each line of the current file here */ | ||
| 1047 | for (i = 0; i <= num_flines; i++) { | ||
| 1048 | strcpy(current_line, process_regex_on_line(flines[i], pattern)); | ||
| 1049 | flines[i] = (char *) bb_xstrndup(current_line, sizeof(char) * (strlen(current_line)+1)); | ||
| 1050 | |||
| 1051 | if (match_found) { | ||
| 1052 | match_lines[j] = i; | ||
| 1053 | j++; | ||
| 1054 | } | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | num_matches = j; | ||
| 1058 | |||
| 1059 | if ((match_lines[0] != -1) && (num_flines > height - 2)) | ||
| 1060 | buffer_line(match_lines[0]); | ||
| 1061 | else | ||
| 1062 | buffer_init(); | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | char *process_regex_on_line(char *line, regex_t *pattern) { | ||
| 1066 | /* This function takes the regex and applies it to the line. | ||
| 1067 | Each part of the line that matches has the HIGHLIGHT | ||
| 1068 | and NORMAL escape sequences placed around it by | ||
| 1069 | insert_highlights, and then the line is returned. */ | ||
| 1070 | |||
| 1071 | int match_status; | ||
| 1072 | char *line2 = (char *) malloc((sizeof(char) * (strlen(line) + 1)) + 64); | ||
| 1073 | char sub_line[256]; | ||
| 1074 | int prev_eo = 0; | ||
| 1075 | memset(sub_line, 0, 256); | ||
| 1076 | strcpy(line2, line); | ||
| 1077 | regmatch_t match_structs; | ||
| 1078 | |||
| 1079 | match_found = 0; | ||
| 1080 | match_status = regexec(pattern, line2, 1, &match_structs, 0); | ||
| 1081 | |||
| 1082 | while (match_status == 0) { | ||
| 1083 | |||
| 1084 | memset(sub_line, 0, 256); | ||
| 1085 | |||
| 1086 | if (match_found == 0) | ||
| 1087 | match_found = 1; | ||
| 1088 | |||
| 1089 | line2 = insert_highlights(line2, match_structs.rm_so + prev_eo, match_structs.rm_eo + prev_eo); | ||
| 1090 | if (match_structs.rm_eo + 11 + prev_eo < strlen(line2)) | ||
| 1091 | strcat(sub_line, line2 + match_structs.rm_eo + 11 + prev_eo); | ||
| 1092 | |||
| 1093 | prev_eo += match_structs.rm_eo + 11; | ||
| 1094 | match_status = regexec(pattern, sub_line, 1, &match_structs, REG_NOTBOL); | ||
| 1095 | } | ||
| 1096 | |||
| 1097 | return line2; | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | char *insert_highlights (char *line, int start, int end) { | ||
| 1101 | |||
| 1102 | char *new_line = (char *) malloc((sizeof(char) * (strlen(line) + 1)) + 10); | ||
| 1103 | memset(new_line, 0, ((sizeof(char) * (strlen(line) + 1)) + 10)); | ||
| 1104 | strncat(new_line, line, start); | ||
| 1105 | strcat(new_line, HIGHLIGHT); | ||
| 1106 | strncat(new_line, line + start, end - start); | ||
| 1107 | strcat(new_line, NORMAL); | ||
| 1108 | strncat(new_line, line + end, strlen(line) - end); | ||
| 1109 | |||
| 1110 | return new_line; | ||
| 1111 | } | ||
| 1112 | |||
| 1113 | static void goto_match(int match) { | ||
| 1114 | |||
| 1115 | /* This goes to a specific match - all line positions of matches are | ||
| 1116 | stored within the match_lines[] array. */ | ||
| 1117 | if ((match < num_matches) && (match >= 0)) { | ||
| 1118 | buffer_line(match_lines[match]); | ||
| 1119 | match_pos = match; | ||
| 1120 | } | ||
| 1121 | } | ||
| 1122 | |||
| 1123 | static void search_backwards() { | ||
| 1124 | |||
| 1125 | int current_linepos = line_pos; | ||
| 1126 | int i; | ||
| 1127 | |||
| 1128 | match_backwards = 1; | ||
| 1129 | regex_process(); | ||
| 1130 | |||
| 1131 | for (i = 0; i < num_matches; i++) { | ||
| 1132 | if (match_lines[i] > current_linepos) { | ||
| 1133 | buffer_line(match_lines[i - num_back_match]); | ||
| 1134 | break; | ||
| 1135 | } | ||
| 1136 | } | ||
| 1137 | |||
| 1138 | /* Reset variables */ | ||
| 1139 | match_backwards = 0; | ||
| 1140 | num_back_match = 1; | ||
| 1141 | |||
| 1142 | } | ||
| 1143 | #endif | ||
| 1144 | 987 | ||
| 1145 | #ifdef CONFIG_FEATURE_LESS_BRACKETS | 988 | #ifdef CONFIG_FEATURE_LESS_BRACKETS |
| 1146 | 989 | ||
| @@ -1166,10 +1009,10 @@ static char opp_bracket (char bracket) { | |||
| 1166 | } | 1009 | } |
| 1167 | 1010 | ||
| 1168 | static void match_right_bracket(char bracket) { | 1011 | static void match_right_bracket(char bracket) { |
| 1169 | 1012 | ||
| 1170 | int bracket_line = -1; | 1013 | int bracket_line = -1; |
| 1171 | int i; | 1014 | int i; |
| 1172 | 1015 | ||
| 1173 | if (strchr(flines[line_pos], bracket) == NULL) { | 1016 | if (strchr(flines[line_pos], bracket) == NULL) { |
| 1174 | clear_line(); | 1017 | clear_line(); |
| 1175 | printf("%s%s%s", HIGHLIGHT, "No bracket in top line", NORMAL); | 1018 | printf("%s%s%s", HIGHLIGHT, "No bracket in top line", NORMAL); |
| @@ -1193,7 +1036,7 @@ static void match_right_bracket(char bracket) { | |||
| 1193 | } | 1036 | } |
| 1194 | 1037 | ||
| 1195 | static void match_left_bracket (char bracket) { | 1038 | static void match_left_bracket (char bracket) { |
| 1196 | 1039 | ||
| 1197 | int bracket_line = -1; | 1040 | int bracket_line = -1; |
| 1198 | int i; | 1041 | int i; |
| 1199 | 1042 | ||
| @@ -1215,19 +1058,130 @@ static void match_left_bracket (char bracket) { | |||
| 1215 | clear_line(); | 1058 | clear_line(); |
| 1216 | printf("%s%s%s", HIGHLIGHT, "No matching bracket found", NORMAL); | 1059 | printf("%s%s%s", HIGHLIGHT, "No matching bracket found", NORMAL); |
| 1217 | } | 1060 | } |
| 1218 | 1061 | ||
| 1219 | buffer_line(bracket_line); | 1062 | buffer_line(bracket_line); |
| 1220 | buffer_print(); | 1063 | buffer_print(); |
| 1221 | } | 1064 | } |
| 1222 | } | 1065 | } |
| 1223 | 1066 | ||
| 1067 | #endif /* CONFIG_FEATURE_LESS_BRACKETS */ | ||
| 1068 | |||
| 1069 | static void keypress_process(int keypress) { | ||
| 1070 | switch (keypress) { | ||
| 1071 | case KEY_DOWN: case 'e': case 'j': case '\015': | ||
| 1072 | buffer_down(1); | ||
| 1073 | buffer_print(); | ||
| 1074 | break; | ||
| 1075 | case KEY_UP: case 'y': case 'k': | ||
| 1076 | buffer_up(1); | ||
| 1077 | buffer_print(); | ||
| 1078 | break; | ||
| 1079 | case PAGE_DOWN: case ' ': case 'z': | ||
| 1080 | buffer_down(height - 1); | ||
| 1081 | buffer_print(); | ||
| 1082 | break; | ||
| 1083 | case PAGE_UP: case 'w': case 'b': | ||
| 1084 | buffer_up(height - 1); | ||
| 1085 | buffer_print(); | ||
| 1086 | break; | ||
| 1087 | case 'd': | ||
| 1088 | buffer_down((height - 1) / 2); | ||
| 1089 | buffer_print(); | ||
| 1090 | break; | ||
| 1091 | case 'u': | ||
| 1092 | buffer_up((height - 1) / 2); | ||
| 1093 | buffer_print(); | ||
| 1094 | break; | ||
| 1095 | case 'g': case 'p': case '<': case '%': | ||
| 1096 | buffer_up(num_flines + 1); | ||
| 1097 | buffer_print(); | ||
| 1098 | break; | ||
| 1099 | case 'G': case '>': | ||
| 1100 | buffer_down(num_flines + 1); | ||
| 1101 | buffer_print(); | ||
| 1102 | break; | ||
| 1103 | case 'q': case 'Q': | ||
| 1104 | tless_exit(0); | ||
| 1105 | break; | ||
| 1106 | #ifdef CONFIG_FEATURE_LESS_MARKS | ||
| 1107 | case 'm': | ||
| 1108 | add_mark(); | ||
| 1109 | buffer_print(); | ||
| 1110 | break; | ||
| 1111 | case '\'': | ||
| 1112 | goto_mark(); | ||
| 1113 | buffer_print(); | ||
| 1114 | break; | ||
| 1115 | #endif | ||
| 1116 | case 'r': | ||
| 1117 | buffer_print(); | ||
| 1118 | break; | ||
| 1119 | case 'R': | ||
| 1120 | full_repaint(); | ||
| 1121 | break; | ||
| 1122 | case 's': | ||
| 1123 | if (inp_stdin) | ||
| 1124 | save_input_to_file(); | ||
| 1125 | break; | ||
| 1126 | case 'E': | ||
| 1127 | examine_file(); | ||
| 1128 | break; | ||
| 1129 | #ifdef CONFIG_FEATURE_LESS_FLAGS | ||
| 1130 | case '=': | ||
| 1131 | clear_line(); | ||
| 1132 | m_status_print(); | ||
| 1133 | break; | ||
| 1224 | #endif | 1134 | #endif |
| 1135 | #ifdef CONFIG_FEATURE_LESS_REGEXP | ||
| 1136 | case '/': | ||
| 1137 | regex_process(); | ||
| 1138 | buffer_print(); | ||
| 1139 | break; | ||
| 1140 | case 'n': | ||
| 1141 | goto_match(match_pos + 1); | ||
| 1142 | buffer_print(); | ||
| 1143 | break; | ||
| 1144 | case 'N': | ||
| 1145 | goto_match(match_pos - 1); | ||
| 1146 | buffer_print(); | ||
| 1147 | break; | ||
| 1148 | case '?': | ||
| 1149 | search_backwards(); | ||
| 1150 | buffer_print(); | ||
| 1151 | break; | ||
| 1152 | #endif | ||
| 1153 | #ifdef CONFIG_FEATURE_LESS_FLAGCS | ||
| 1154 | case '-': | ||
| 1155 | flag_change(); | ||
| 1156 | buffer_print(); | ||
| 1157 | break; | ||
| 1158 | case '_': | ||
| 1159 | show_flag_status(); | ||
| 1160 | break; | ||
| 1161 | #endif | ||
| 1162 | #ifdef CONFIG_FEATURE_LESS_BRACKETS | ||
| 1163 | case '{': case '(': case '[': | ||
| 1164 | match_right_bracket(keypress); | ||
| 1165 | break; | ||
| 1166 | case '}': case ')': case ']': | ||
| 1167 | match_left_bracket(keypress); | ||
| 1168 | break; | ||
| 1169 | #endif | ||
| 1170 | case ':': | ||
| 1171 | colon_process(); | ||
| 1172 | break; | ||
| 1173 | default: | ||
| 1174 | break; | ||
| 1175 | } | ||
| 1176 | if (isdigit(keypress)) | ||
| 1177 | number_process(keypress); | ||
| 1178 | } | ||
| 1225 | 1179 | ||
| 1226 | int less_main(int argc, char **argv) { | 1180 | int less_main(int argc, char **argv) { |
| 1227 | 1181 | ||
| 1228 | unsigned long flags; | 1182 | unsigned long flags; |
| 1229 | int keypress; | 1183 | int keypress; |
| 1230 | 1184 | ||
| 1231 | flags = bb_getopt_ulflags(argc, argv, "EMNm~"); | 1185 | flags = bb_getopt_ulflags(argc, argv, "EMNm~"); |
| 1232 | E_FLAG = (flags & 1); | 1186 | E_FLAG = (flags & 1); |
| 1233 | M_FLAG = (flags & 2); | 1187 | M_FLAG = (flags & 2); |
| @@ -1239,7 +1193,7 @@ int less_main(int argc, char **argv) { | |||
| 1239 | argv += optind; | 1193 | argv += optind; |
| 1240 | files = argv; | 1194 | files = argv; |
| 1241 | num_files = argc; | 1195 | num_files = argc; |
| 1242 | 1196 | ||
| 1243 | if (!num_files) { | 1197 | if (!num_files) { |
| 1244 | if (ttyname(STDIN_FILENO) == NULL) | 1198 | if (ttyname(STDIN_FILENO) == NULL) |
| 1245 | inp_stdin = 1; | 1199 | inp_stdin = 1; |
| @@ -1248,13 +1202,13 @@ int less_main(int argc, char **argv) { | |||
| 1248 | bb_show_usage(); | 1202 | bb_show_usage(); |
| 1249 | } | 1203 | } |
| 1250 | } | 1204 | } |
| 1251 | 1205 | ||
| 1252 | strcpy(filename, (inp_stdin) ? "stdin" : files[0]); | 1206 | strcpy(filename, (inp_stdin) ? "stdin" : files[0]); |
| 1253 | tty_width_height(); | 1207 | tty_width_height(); |
| 1254 | data_readlines(); | 1208 | data_readlines(); |
| 1255 | buffer_init(); | 1209 | buffer_init(); |
| 1256 | buffer_print(); | 1210 | buffer_print(); |
| 1257 | 1211 | ||
| 1258 | while (1) { | 1212 | while (1) { |
| 1259 | keypress = tless_getch(); | 1213 | keypress = tless_getch(); |
| 1260 | keypress_process(keypress); | 1214 | keypress_process(keypress); |
