diff options
author | Rob Landley <rob@landley.net> | 2005-09-18 00:58:49 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2005-09-18 00:58:49 +0000 |
commit | d57ae8b796e8793a1c7864f91d5f191e544718a5 (patch) | |
tree | 6fec694b5e840abf27ea38d16b7c1d983e69068b /miscutils/less.c | |
parent | 07e42dc05612653f83d31fcea325afbe38182c1f (diff) | |
download | busybox-w32-d57ae8b796e8793a1c7864f91d5f191e544718a5.tar.gz busybox-w32-d57ae8b796e8793a1c7864f91d5f191e544718a5.tar.bz2 busybox-w32-d57ae8b796e8793a1c7864f91d5f191e544718a5.zip |
First cleanup pass, from Rob Sullivan. More to be done...
Diffstat (limited to 'miscutils/less.c')
-rw-r--r-- | miscutils/less.c | 250 |
1 files changed, 106 insertions, 144 deletions
diff --git a/miscutils/less.c b/miscutils/less.c index d7898caab..3fd22ca19 100644 --- a/miscutils/less.c +++ b/miscutils/less.c | |||
@@ -89,8 +89,8 @@ static int height; | |||
89 | static int width; | 89 | static int width; |
90 | static char **files; | 90 | static char **files; |
91 | static char filename[256]; | 91 | static char filename[256]; |
92 | static char buffer[100][256]; | 92 | static char **buffer; |
93 | static char *flines[MAXLINES]; | 93 | static char **flines; |
94 | static int current_file = 1; | 94 | static int current_file = 1; |
95 | static int line_pos; | 95 | static int line_pos; |
96 | static int num_flines; | 96 | static int num_flines; |
@@ -98,11 +98,12 @@ static int num_files = 1; | |||
98 | static int past_eof; | 98 | static int past_eof; |
99 | 99 | ||
100 | /* Command line options */ | 100 | /* Command line options */ |
101 | static int E_FLAG; | 101 | static unsigned long flags; |
102 | static int M_FLAG; | 102 | #define FLAG_E 1 |
103 | static int N_FLAG; | 103 | #define FLAG_M (1<<1) |
104 | static int m_FLAG; | 104 | #define FLAG_m (1<<2) |
105 | static int TILDE_FLAG; | 105 | #define FLAG_N (1<<3) |
106 | #define FLAG_TILDE (1<<4) | ||
106 | 107 | ||
107 | /* This is needed so that program behaviour changes when input comes from | 108 | /* This is needed so that program behaviour changes when input comes from |
108 | stdin */ | 109 | stdin */ |
@@ -139,7 +140,7 @@ static void set_tty_cooked(void) { | |||
139 | tcsetattr(0, TCSANOW, &term_orig); | 140 | tcsetattr(0, TCSANOW, &term_orig); |
140 | } | 141 | } |
141 | 142 | ||
142 | /* Set terminal input to raw mode */ | 143 | /* Set terminal input to raw mode (taken from vi.c) */ |
143 | static void set_tty_raw(void) { | 144 | static void set_tty_raw(void) { |
144 | tcgetattr(0, &term_orig); | 145 | tcgetattr(0, &term_orig); |
145 | term_vi = term_orig; | 146 | term_vi = term_orig; |
@@ -221,8 +222,8 @@ static void add_linenumbers(void) { | |||
221 | 222 | ||
222 | for (i = 0; i <= num_flines; i++) { | 223 | for (i = 0; i <= num_flines; i++) { |
223 | safe_strncpy(current_line, flines[i], 256); | 224 | safe_strncpy(current_line, flines[i], 256); |
224 | flines[i] = xrealloc(flines[i], strlen(current_line) + 7 ); | 225 | flines[i] = xrealloc(flines[i], strlen(current_line) + 7); |
225 | sprintf(flines[i],"%5d %s", i+1, current_line); | 226 | sprintf(flines[i],"%5d %s", i + 1, current_line); |
226 | } | 227 | } |
227 | } | 228 | } |
228 | 229 | ||
@@ -234,6 +235,13 @@ static void data_readlines(void) { | |||
234 | 235 | ||
235 | fp = (inp_stdin) ? stdin : bb_xfopen(filename, "rt"); | 236 | fp = (inp_stdin) ? stdin : bb_xfopen(filename, "rt"); |
236 | 237 | ||
238 | /* First of all, we need to know the number of lines so that flines can be initialised. */ | ||
239 | for (i = 0; (!feof(fp)) && (i <= MAXLINES); i++) | ||
240 | fgets(current_line, 256, fp); | ||
241 | rewind(fp); | ||
242 | /* Initialise fp */ | ||
243 | flines = malloc(i * sizeof(char *)); | ||
244 | |||
237 | for (i = 0; (!feof(fp)) && (i <= MAXLINES); i++) { | 245 | for (i = 0; (!feof(fp)) && (i <= MAXLINES); i++) { |
238 | strcpy(current_line, ""); | 246 | strcpy(current_line, ""); |
239 | fgets(current_line, 256, fp); | 247 | fgets(current_line, 256, fp); |
@@ -249,36 +257,17 @@ static void data_readlines(void) { | |||
249 | 257 | ||
250 | fclose(fp); | 258 | fclose(fp); |
251 | 259 | ||
252 | if (inp_stdin) | 260 | inp = (inp_stdin) ? fopen(CURRENT_TTY, "r") : stdin; |
253 | inp = fopen(CURRENT_TTY, "r"); | 261 | |
254 | else | ||
255 | inp = stdin; | ||
256 | |||
257 | if (ea_inp_stdin) { | 262 | if (ea_inp_stdin) { |
258 | fclose(inp); | 263 | fclose(inp); |
259 | inp = fopen(CURRENT_TTY, "r"); | 264 | inp = fopen(CURRENT_TTY, "r"); |
260 | } | 265 | } |
261 | 266 | ||
262 | if (N_FLAG) | 267 | if (flags & FLAG_N) |
263 | add_linenumbers(); | 268 | add_linenumbers(); |
264 | } | 269 | } |
265 | 270 | ||
266 | /* Free the file data */ | ||
267 | static void free_flines(void) { | ||
268 | |||
269 | int i; | ||
270 | |||
271 | for (i = 0; i <= num_flines; i++) | ||
272 | free(flines[i]); | ||
273 | } | ||
274 | |||
275 | #ifdef CONFIG_FEATURE_LESS_FLAGS | ||
276 | /* Calculate the percentage the current line position is through the file */ | ||
277 | static int calc_percent(void) { | ||
278 | return ((100 * (line_pos + height - 2) / num_flines) + 1); | ||
279 | } | ||
280 | #endif | ||
281 | |||
282 | /* Turn a percentage into a line number */ | 271 | /* Turn a percentage into a line number */ |
283 | static int reverse_percent(int percentage) { | 272 | static int reverse_percent(int percentage) { |
284 | double linenum = percentage; | 273 | double linenum = percentage; |
@@ -287,6 +276,13 @@ static int reverse_percent(int percentage) { | |||
287 | } | 276 | } |
288 | 277 | ||
289 | #ifdef CONFIG_FEATURE_LESS_FLAGS | 278 | #ifdef CONFIG_FEATURE_LESS_FLAGS |
279 | |||
280 | /* Interestingly, writing calc_percent as a function and not a prototype saves around 32 bytes | ||
281 | * on my build. */ | ||
282 | static int calc_percent(void) { | ||
283 | return ((100 * (line_pos + height - 2) / num_flines) + 1); | ||
284 | } | ||
285 | |||
290 | /* Print a status line if -M was specified */ | 286 | /* Print a status line if -M was specified */ |
291 | static void m_status_print(void) { | 287 | static void m_status_print(void) { |
292 | 288 | ||
@@ -342,9 +338,9 @@ static void status_print(void) { | |||
342 | 338 | ||
343 | /* Change the status if flags have been set */ | 339 | /* Change the status if flags have been set */ |
344 | #ifdef CONFIG_FEATURE_LESS_FLAGS | 340 | #ifdef CONFIG_FEATURE_LESS_FLAGS |
345 | if (M_FLAG) | 341 | if (flags & FLAG_M) |
346 | m_status_print(); | 342 | m_status_print(); |
347 | else if (m_FLAG) | 343 | else if (flags & FLAG_m) |
348 | medium_status_print(); | 344 | medium_status_print(); |
349 | /* No flags set */ | 345 | /* No flags set */ |
350 | else { | 346 | else { |
@@ -377,7 +373,6 @@ static void buffer_print(void) { | |||
377 | move_cursor(0,0); | 373 | move_cursor(0,0); |
378 | for (i = 0; i < height - 1; i++) | 374 | for (i = 0; i < height - 1; i++) |
379 | printf("%s", buffer[i]); | 375 | printf("%s", buffer[i]); |
380 | status_print(); | ||
381 | } | 376 | } |
382 | else { | 377 | else { |
383 | printf("%s", CLEAR); | 378 | printf("%s", CLEAR); |
@@ -386,8 +381,9 @@ static void buffer_print(void) { | |||
386 | putchar('\n'); | 381 | putchar('\n'); |
387 | for (i = 0; i < height - 1; i++) | 382 | for (i = 0; i < height - 1; i++) |
388 | printf("%s", buffer[i]); | 383 | printf("%s", buffer[i]); |
389 | status_print(); | ||
390 | } | 384 | } |
385 | |||
386 | status_print(); | ||
391 | } | 387 | } |
392 | 388 | ||
393 | /* Initialise the buffer */ | 389 | /* Initialise the buffer */ |
@@ -395,18 +391,18 @@ static void buffer_init(void) { | |||
395 | 391 | ||
396 | int i; | 392 | int i; |
397 | 393 | ||
398 | for (i = 0; i < (height - 1); i++) | 394 | /* malloc the number of lines needed for the buffer */ |
399 | memset(buffer[i], '\0', 256); | 395 | buffer = xrealloc(buffer, height * sizeof(char *)); |
400 | 396 | ||
401 | /* Fill the buffer until the end of the file or the | 397 | /* Fill the buffer until the end of the file or the |
402 | end of the buffer is reached */ | 398 | end of the buffer is reached */ |
403 | for (i = 0; (i < (height - 1)) && (i <= num_flines); i++) { | 399 | for (i = 0; (i < (height - 1)) && (i <= num_flines); i++) { |
404 | strcpy(buffer[i], flines[i]); | 400 | buffer[i] = (char *) bb_xstrdup(flines[i]); |
405 | } | 401 | } |
406 | 402 | ||
407 | /* If the buffer still isn't full, fill it with blank lines */ | 403 | /* If the buffer still isn't full, fill it with blank lines */ |
408 | for (; i < (height - 1); i++) { | 404 | for (; i < (height - 1); i++) { |
409 | strcpy(buffer[i], ""); | 405 | buffer[i] = ""; |
410 | } | 406 | } |
411 | } | 407 | } |
412 | 408 | ||
@@ -419,7 +415,7 @@ static void buffer_down(int nlines) { | |||
419 | if (line_pos + (height - 3) + nlines < num_flines) { | 415 | if (line_pos + (height - 3) + nlines < num_flines) { |
420 | line_pos += nlines; | 416 | line_pos += nlines; |
421 | for (i = 0; i < (height - 1); i++) | 417 | for (i = 0; i < (height - 1); i++) |
422 | strcpy(buffer[i], flines[line_pos + i]); | 418 | buffer[i] = (char *) bb_xstrdup(flines[line_pos + i]); |
423 | } | 419 | } |
424 | else { | 420 | else { |
425 | /* As the number of lines requested was too large, we just move | 421 | /* As the number of lines requested was too large, we just move |
@@ -427,12 +423,12 @@ static void buffer_down(int nlines) { | |||
427 | while (line_pos + (height - 3) + 1 < num_flines) { | 423 | while (line_pos + (height - 3) + 1 < num_flines) { |
428 | line_pos += 1; | 424 | line_pos += 1; |
429 | for (i = 0; i < (height - 1); i++) | 425 | for (i = 0; i < (height - 1); i++) |
430 | strcpy(buffer[i], flines[line_pos + i]); | 426 | buffer[i] = (char *) bb_xstrdup(flines[line_pos + i]); |
431 | } | 427 | } |
432 | } | 428 | } |
433 | 429 | ||
434 | /* We exit if the -E flag has been set */ | 430 | /* We exit if the -E flag has been set */ |
435 | if (E_FLAG && (line_pos + (height - 2) == num_flines)) | 431 | if ((flags & FLAG_E) && (line_pos + (height - 2) == num_flines)) |
436 | tless_exit(0); | 432 | tless_exit(0); |
437 | } | 433 | } |
438 | } | 434 | } |
@@ -446,7 +442,7 @@ static void buffer_up(int nlines) { | |||
446 | if (line_pos - nlines >= 0) { | 442 | if (line_pos - nlines >= 0) { |
447 | line_pos -= nlines; | 443 | line_pos -= nlines; |
448 | for (i = 0; i < (height - 1); i++) | 444 | for (i = 0; i < (height - 1); i++) |
449 | strcpy(buffer[i], flines[line_pos + i]); | 445 | buffer[i] = (char *) bb_xstrdup(flines[line_pos + i]); |
450 | } | 446 | } |
451 | else { | 447 | else { |
452 | /* As the requested number of lines to move was too large, we | 448 | /* As the requested number of lines to move was too large, we |
@@ -454,7 +450,7 @@ static void buffer_up(int nlines) { | |||
454 | while (line_pos != 0) { | 450 | while (line_pos != 0) { |
455 | line_pos -= 1; | 451 | line_pos -= 1; |
456 | for (i = 0; i < (height - 1); i++) | 452 | for (i = 0; i < (height - 1); i++) |
457 | strcpy(buffer[i], flines[line_pos + i]); | 453 | buffer[i] = (char *) bb_xstrdup(flines[line_pos + i]); |
458 | } | 454 | } |
459 | } | 455 | } |
460 | } | 456 | } |
@@ -474,10 +470,10 @@ static void buffer_up(int nlines) { | |||
474 | is past the EOF */ | 470 | is past the EOF */ |
475 | for (i = 0; i < (height - 1); i++) { | 471 | for (i = 0; i < (height - 1); i++) { |
476 | if (i < tilde_line - nlines + 1) | 472 | if (i < tilde_line - nlines + 1) |
477 | strcpy(buffer[i], flines[line_pos + i]); | 473 | buffer[i] = (char *) bb_xstrdup(flines[line_pos + i]); |
478 | else { | 474 | else { |
479 | if (line_pos >= num_flines - height + 2) | 475 | if (line_pos >= num_flines - height + 2) |
480 | strcpy(buffer[i], "~\n"); | 476 | buffer[i] = "~\n"; |
481 | } | 477 | } |
482 | } | 478 | } |
483 | } | 479 | } |
@@ -496,15 +492,15 @@ static void buffer_line(int linenum) { | |||
496 | } | 492 | } |
497 | else if (linenum < (num_flines - height - 2)) { | 493 | else if (linenum < (num_flines - height - 2)) { |
498 | for (i = 0; i < (height - 1); i++) | 494 | for (i = 0; i < (height - 1); i++) |
499 | strcpy(buffer[i], flines[linenum + i]); | 495 | buffer[i] = (char *) bb_xstrdup(flines[linenum + i]); |
500 | line_pos = linenum; | 496 | line_pos = linenum; |
501 | } | 497 | } |
502 | else { | 498 | else { |
503 | for (i = 0; i < (height - 1); i++) { | 499 | for (i = 0; i < (height - 1); i++) { |
504 | if (linenum + i < num_flines + 2) | 500 | if (linenum + i < num_flines + 2) |
505 | strcpy(buffer[i], flines[linenum + i]); | 501 | buffer[i] = (char *) bb_xstrdup(flines[linenum + i]); |
506 | else | 502 | else |
507 | strcpy(buffer[i], (TILDE_FLAG) ? "\n" : "~\n"); | 503 | buffer[i] = (char *) bb_xstrdup((flags & FLAG_TILDE) ? "\n" : "~\n"); |
508 | } | 504 | } |
509 | line_pos = linenum; | 505 | line_pos = linenum; |
510 | /* Set past_eof so buffer_down and buffer_up act differently */ | 506 | /* Set past_eof so buffer_down and buffer_up act differently */ |
@@ -512,6 +508,20 @@ static void buffer_line(int linenum) { | |||
512 | } | 508 | } |
513 | } | 509 | } |
514 | 510 | ||
511 | /* Reinitialise everything for a new file - free the memory and start over */ | ||
512 | static void reinitialise(void) { | ||
513 | |||
514 | int i; | ||
515 | |||
516 | for (i = 0; i <= num_flines; i++) | ||
517 | free(flines[i]); | ||
518 | free(flines); | ||
519 | |||
520 | data_readlines(); | ||
521 | buffer_init(); | ||
522 | buffer_print(); | ||
523 | } | ||
524 | |||
515 | static void examine_file(void) { | 525 | static void examine_file(void) { |
516 | 526 | ||
517 | int newline_offset; | 527 | int newline_offset; |
@@ -531,52 +541,23 @@ static void examine_file(void) { | |||
531 | 541 | ||
532 | inp_stdin = 0; | 542 | inp_stdin = 0; |
533 | ea_inp_stdin = 1; | 543 | ea_inp_stdin = 1; |
534 | free_flines(); | 544 | reinitialise(); |
535 | data_readlines(); | ||
536 | buffer_init(); | ||
537 | buffer_print(); | ||
538 | } | ||
539 | |||
540 | |||
541 | static void next_file(void) { | ||
542 | if (current_file != num_files) { | ||
543 | current_file++; | ||
544 | strcpy(filename, files[current_file - 1]); | ||
545 | free_flines(); | ||
546 | data_readlines(); | ||
547 | buffer_init(); | ||
548 | buffer_print(); | ||
549 | } | ||
550 | else { | ||
551 | clear_line(); | ||
552 | printf("%s%s%s", HIGHLIGHT, "No next file", NORMAL); | ||
553 | } | ||
554 | } | 545 | } |
555 | 546 | ||
556 | static void previous_file(void) { | 547 | /* This function changes the file currently being paged. direction can be one of the following: |
557 | if (current_file != 1) { | 548 | * -1: go back one file |
558 | current_file--; | 549 | * 0: go to the first file |
550 | * 1: go forward one file | ||
551 | */ | ||
552 | static void change_file (int direction) { | ||
553 | if (current_file != ((direction > 0) ? num_files : 1)) { | ||
554 | current_file = direction ? current_file + direction : 1; | ||
559 | strcpy(filename, files[current_file - 1]); | 555 | strcpy(filename, files[current_file - 1]); |
560 | 556 | reinitialise(); | |
561 | free_flines(); | ||
562 | data_readlines(); | ||
563 | buffer_init(); | ||
564 | buffer_print(); | ||
565 | } | 557 | } |
566 | else { | 558 | else { |
567 | clear_line(); | 559 | clear_line(); |
568 | printf("%s%s%s", HIGHLIGHT, "No previous file", NORMAL); | 560 | printf("%s%s%s", HIGHLIGHT, (direction > 0) ? "No next file" : "No previous file", NORMAL); |
569 | } | ||
570 | } | ||
571 | |||
572 | static void first_file(void) { | ||
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 | } | 561 | } |
581 | } | 562 | } |
582 | 563 | ||
@@ -585,14 +566,14 @@ static void remove_current_file(void) { | |||
585 | int i; | 566 | int i; |
586 | 567 | ||
587 | if (current_file != 1) { | 568 | if (current_file != 1) { |
588 | previous_file(); | 569 | change_file(-1); |
589 | for (i = 3; i <= num_files; i++) | 570 | for (i = 3; i <= num_files; i++) |
590 | files[i - 2] = files[i - 1]; | 571 | files[i - 2] = files[i - 1]; |
591 | num_files--; | 572 | num_files--; |
592 | buffer_print(); | 573 | buffer_print(); |
593 | } | 574 | } |
594 | else { | 575 | else { |
595 | next_file(); | 576 | change_file(1); |
596 | for (i = 2; i <= num_files; i++) | 577 | for (i = 2; i <= num_files; i++) |
597 | files[i - 2] = files[i - 1]; | 578 | files[i - 2] = files[i - 1]; |
598 | num_files--; | 579 | num_files--; |
@@ -624,16 +605,16 @@ static void colon_process(void) { | |||
624 | break; | 605 | break; |
625 | #endif | 606 | #endif |
626 | case 'n': | 607 | case 'n': |
627 | next_file(); | 608 | change_file(1); |
628 | break; | 609 | break; |
629 | case 'p': | 610 | case 'p': |
630 | previous_file(); | 611 | change_file(-1); |
631 | break; | 612 | break; |
632 | case 'q': | 613 | case 'q': |
633 | tless_exit(0); | 614 | tless_exit(0); |
634 | break; | 615 | break; |
635 | case 'x': | 616 | case 'x': |
636 | first_file(); | 617 | change_file(0); |
637 | break; | 618 | break; |
638 | default: | 619 | default: |
639 | break; | 620 | break; |
@@ -715,10 +696,7 @@ static void regex_process(void) { | |||
715 | 696 | ||
716 | /* Get the uncompiled regular expression from the user */ | 697 | /* Get the uncompiled regular expression from the user */ |
717 | clear_line(); | 698 | clear_line(); |
718 | if (match_backwards) | 699 | putchar((match_backwards) ? '?' : '/'); |
719 | printf("?"); | ||
720 | else | ||
721 | printf("/"); | ||
722 | scanf("%s", uncomp_regex); | 700 | scanf("%s", uncomp_regex); |
723 | 701 | ||
724 | /* Compile the regex and check for errors */ | 702 | /* Compile the regex and check for errors */ |
@@ -728,7 +706,6 @@ static void regex_process(void) { | |||
728 | for (i = 0; i <= num_flines; i++) { | 706 | for (i = 0; i <= num_flines; i++) { |
729 | strcpy(current_line, process_regex_on_line(flines[i], pattern)); | 707 | strcpy(current_line, process_regex_on_line(flines[i], pattern)); |
730 | flines[i] = (char *) bb_xstrndup(current_line, sizeof(char) * (strlen(current_line)+1)); | 708 | flines[i] = (char *) bb_xstrndup(current_line, sizeof(char) * (strlen(current_line)+1)); |
731 | |||
732 | if (match_found) { | 709 | if (match_found) { |
733 | match_lines[j] = i; | 710 | match_lines[j] = i; |
734 | j++; | 711 | j++; |
@@ -789,7 +766,7 @@ static void number_process(int first_digit) { | |||
789 | 766 | ||
790 | /* Receive input until a letter is given */ | 767 | /* Receive input until a letter is given */ |
791 | while((num_input[i] = tless_getch()) && isdigit(num_input[i])) { | 768 | while((num_input[i] = tless_getch()) && isdigit(num_input[i])) { |
792 | printf("%c",num_input[i]); | 769 | printf("%c", num_input[i]); |
793 | i++; | 770 | i++; |
794 | } | 771 | } |
795 | 772 | ||
@@ -803,40 +780,35 @@ static void number_process(int first_digit) { | |||
803 | switch (keypress) { | 780 | switch (keypress) { |
804 | case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015': | 781 | case KEY_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015': |
805 | buffer_down(num); | 782 | buffer_down(num); |
806 | buffer_print(); | ||
807 | break; | 783 | break; |
808 | case KEY_UP: case 'b': case 'w': case 'y': case 'u': | 784 | case KEY_UP: case 'b': case 'w': case 'y': case 'u': |
809 | buffer_up(num); | 785 | buffer_up(num); |
810 | buffer_print(); | ||
811 | break; | 786 | break; |
812 | case 'g': case '<': case 'G': case '>': | 787 | case 'g': case '<': case 'G': case '>': |
813 | if (num_flines >= height - 2) | 788 | if (num_flines >= height - 2) |
814 | buffer_line(num - 1); | 789 | buffer_line(num - 1); |
815 | buffer_print(); | ||
816 | break; | 790 | break; |
817 | case 'p': case '%': | 791 | case 'p': case '%': |
818 | buffer_line(reverse_percent(num)); | 792 | buffer_line(reverse_percent(num)); |
819 | buffer_print(); | ||
820 | break; | 793 | break; |
821 | #ifdef CONFIG_FEATURE_LESS_REGEXP | 794 | #ifdef CONFIG_FEATURE_LESS_REGEXP |
822 | case 'n': | 795 | case 'n': |
823 | goto_match(match_pos + num - 1); | 796 | goto_match(match_pos + num - 1); |
824 | buffer_print(); | ||
825 | break; | 797 | break; |
826 | case '/': | 798 | case '/': |
827 | regex_process(); | 799 | regex_process(); |
828 | goto_match(num - 1); | 800 | goto_match(num - 1); |
829 | buffer_print(); | ||
830 | break; | 801 | break; |
831 | case '?': | 802 | case '?': |
832 | num_back_match = num; | 803 | num_back_match = num; |
833 | search_backwards(); | 804 | search_backwards(); |
834 | buffer_print(); | ||
835 | break; | 805 | break; |
836 | #endif | 806 | #endif |
837 | default: | 807 | default: |
838 | break; | 808 | break; |
839 | } | 809 | } |
810 | |||
811 | buffer_print(); | ||
840 | } | 812 | } |
841 | 813 | ||
842 | #ifdef CONFIG_FEATURE_LESS_FLAGCS | 814 | #ifdef CONFIG_FEATURE_LESS_FLAGCS |
@@ -845,21 +817,21 @@ static void flag_change(void) { | |||
845 | int keypress; | 817 | int keypress; |
846 | 818 | ||
847 | clear_line(); | 819 | clear_line(); |
848 | printf("-"); | 820 | putchar('-'); |
849 | keypress = tless_getch(); | 821 | keypress = tless_getch(); |
850 | 822 | ||
851 | switch (keypress) { | 823 | switch (keypress) { |
852 | case 'M': | 824 | case 'M': |
853 | M_FLAG = !M_FLAG; | 825 | flags &= ~FLAG_M; |
854 | break; | 826 | break; |
855 | case 'm': | 827 | case 'm': |
856 | m_FLAG = !m_FLAG; | 828 | flags &= ~FLAG_m; |
857 | break; | 829 | break; |
858 | case 'E': | 830 | case 'E': |
859 | E_FLAG = !E_FLAG; | 831 | flags &= ~FLAG_E; |
860 | break; | 832 | break; |
861 | case '~': | 833 | case '~': |
862 | TILDE_FLAG = !TILDE_FLAG; | 834 | flags &= ~FLAG_TILDE; |
863 | break; | 835 | break; |
864 | default: | 836 | default: |
865 | break; | 837 | break; |
@@ -872,24 +844,24 @@ static void show_flag_status(void) { | |||
872 | int flag_val; | 844 | int flag_val; |
873 | 845 | ||
874 | clear_line(); | 846 | clear_line(); |
875 | printf("_"); | 847 | putchar('_'); |
876 | keypress = tless_getch(); | 848 | keypress = tless_getch(); |
877 | 849 | ||
878 | switch (keypress) { | 850 | switch (keypress) { |
879 | case 'M': | 851 | case 'M': |
880 | flag_val = M_FLAG; | 852 | flag_val = flags & FLAG_M; |
881 | break; | 853 | break; |
882 | case 'm': | 854 | case 'm': |
883 | flag_val = m_FLAG; | 855 | flag_val = flags & FLAG_m; |
884 | break; | 856 | break; |
885 | case '~': | 857 | case '~': |
886 | flag_val = TILDE_FLAG; | 858 | flag_val = flags & FLAG_TILDE; |
887 | break; | 859 | break; |
888 | case 'N': | 860 | case 'N': |
889 | flag_val = N_FLAG; | 861 | flag_val = flags & FLAG_N; |
890 | break; | 862 | break; |
891 | case 'E': | 863 | case 'E': |
892 | flag_val = E_FLAG; | 864 | flag_val = flags & FLAG_E; |
893 | break; | 865 | break; |
894 | default: | 866 | default: |
895 | flag_val = 0; | 867 | flag_val = 0; |
@@ -897,7 +869,7 @@ static void show_flag_status(void) { | |||
897 | } | 869 | } |
898 | 870 | ||
899 | clear_line(); | 871 | clear_line(); |
900 | printf("%s%s%i%s", HIGHLIGHT, "The status of the flag is: ", flag_val != 0, NORMAL); | 872 | printf("%s%s%i%s", HIGHLIGHT, "The status of the flag is: ", flag_val, NORMAL); |
901 | } | 873 | } |
902 | #endif | 874 | #endif |
903 | 875 | ||
@@ -967,21 +939,19 @@ static void goto_mark(void) { | |||
967 | clear_line(); | 939 | clear_line(); |
968 | printf("Go to mark: "); | 940 | printf("Go to mark: "); |
969 | letter = tless_getch(); | 941 | letter = tless_getch(); |
942 | clear_line(); | ||
943 | |||
970 | if (isalpha(letter)) { | 944 | if (isalpha(letter)) { |
971 | for (i = 0; i <= num_marks; i++) | 945 | for (i = 0; i <= num_marks; i++) |
972 | if (letter == mark_lines[i][0]) { | 946 | if (letter == mark_lines[i][0]) { |
973 | buffer_line(mark_lines[i][1]); | 947 | buffer_line(mark_lines[i][1]); |
974 | break; | 948 | break; |
975 | } | 949 | } |
976 | if ((num_marks == 14) && (letter != mark_lines[14][0])) { | 950 | if ((num_marks == 14) && (letter != mark_lines[14][0])) |
977 | clear_line(); | ||
978 | printf("%s%s%s", HIGHLIGHT, "Mark not set", NORMAL); | 951 | printf("%s%s%s", HIGHLIGHT, "Mark not set", NORMAL); |
979 | } | ||
980 | } | 952 | } |
981 | else { | 953 | else |
982 | clear_line(); | ||
983 | printf("%s%s%s", HIGHLIGHT, "Invalid mark letter", NORMAL); | 954 | printf("%s%s%s", HIGHLIGHT, "Invalid mark letter", NORMAL); |
984 | } | ||
985 | } | 955 | } |
986 | #endif | 956 | #endif |
987 | 957 | ||
@@ -1014,10 +984,10 @@ static void match_right_bracket(char bracket) { | |||
1014 | int bracket_line = -1; | 984 | int bracket_line = -1; |
1015 | int i; | 985 | int i; |
1016 | 986 | ||
1017 | if (strchr(flines[line_pos], bracket) == NULL) { | 987 | clear_line(); |
1018 | clear_line(); | 988 | |
989 | if (strchr(flines[line_pos], bracket) == NULL) | ||
1019 | printf("%s%s%s", HIGHLIGHT, "No bracket in top line", NORMAL); | 990 | printf("%s%s%s", HIGHLIGHT, "No bracket in top line", NORMAL); |
1020 | } | ||
1021 | else { | 991 | else { |
1022 | for (i = line_pos + 1; i < num_flines; i++) { | 992 | for (i = line_pos + 1; i < num_flines; i++) { |
1023 | if (strchr(flines[i], opp_bracket(bracket)) != NULL) { | 993 | if (strchr(flines[i], opp_bracket(bracket)) != NULL) { |
@@ -1026,10 +996,8 @@ static void match_right_bracket(char bracket) { | |||
1026 | } | 996 | } |
1027 | } | 997 | } |
1028 | 998 | ||
1029 | if (bracket_line == -1) { | 999 | if (bracket_line == -1) |
1030 | clear_line(); | ||
1031 | printf("%s%s%s", HIGHLIGHT, "No matching bracket found", NORMAL); | 1000 | printf("%s%s%s", HIGHLIGHT, "No matching bracket found", NORMAL); |
1032 | } | ||
1033 | 1001 | ||
1034 | buffer_line(bracket_line - height + 2); | 1002 | buffer_line(bracket_line - height + 2); |
1035 | buffer_print(); | 1003 | buffer_print(); |
@@ -1041,8 +1009,9 @@ static void match_left_bracket (char bracket) { | |||
1041 | int bracket_line = -1; | 1009 | int bracket_line = -1; |
1042 | int i; | 1010 | int i; |
1043 | 1011 | ||
1012 | clear_line(); | ||
1013 | |||
1044 | if (strchr(flines[line_pos + height - 2], bracket) == NULL) { | 1014 | if (strchr(flines[line_pos + height - 2], bracket) == NULL) { |
1045 | clear_line(); | ||
1046 | printf("%s%s%s", HIGHLIGHT, "No bracket in bottom line", NORMAL); | 1015 | printf("%s%s%s", HIGHLIGHT, "No bracket in bottom line", NORMAL); |
1047 | printf("%s", flines[line_pos + height]); | 1016 | printf("%s", flines[line_pos + height]); |
1048 | sleep(4); | 1017 | sleep(4); |
@@ -1055,10 +1024,8 @@ static void match_left_bracket (char bracket) { | |||
1055 | } | 1024 | } |
1056 | } | 1025 | } |
1057 | 1026 | ||
1058 | if (bracket_line == -1) { | 1027 | if (bracket_line == -1) |
1059 | clear_line(); | ||
1060 | printf("%s%s%s", HIGHLIGHT, "No matching bracket found", NORMAL); | 1028 | printf("%s%s%s", HIGHLIGHT, "No matching bracket found", NORMAL); |
1061 | } | ||
1062 | 1029 | ||
1063 | buffer_line(bracket_line); | 1030 | buffer_line(bracket_line); |
1064 | buffer_print(); | 1031 | buffer_print(); |
@@ -1174,21 +1141,16 @@ static void keypress_process(int keypress) { | |||
1174 | default: | 1141 | default: |
1175 | break; | 1142 | break; |
1176 | } | 1143 | } |
1144 | |||
1177 | if (isdigit(keypress)) | 1145 | if (isdigit(keypress)) |
1178 | number_process(keypress); | 1146 | number_process(keypress); |
1179 | } | 1147 | } |
1180 | 1148 | ||
1181 | int less_main(int argc, char **argv) { | 1149 | int less_main(int argc, char **argv) { |
1182 | 1150 | ||
1183 | unsigned long flags; | ||
1184 | int keypress; | 1151 | int keypress; |
1185 | 1152 | ||
1186 | flags = bb_getopt_ulflags(argc, argv, "EMNm~"); | 1153 | flags = bb_getopt_ulflags(argc, argv, "EMmN~"); |
1187 | E_FLAG = (flags & 1); | ||
1188 | M_FLAG = (flags & 2); | ||
1189 | N_FLAG = (flags & 4); | ||
1190 | m_FLAG = (flags & 8); | ||
1191 | TILDE_FLAG = (flags & 16); | ||
1192 | 1154 | ||
1193 | argc -= optind; | 1155 | argc -= optind; |
1194 | argv += optind; | 1156 | argv += optind; |