diff options
| author | Rob Landley <rob@landley.net> | 2006-04-18 01:53:41 +0000 |
|---|---|---|
| committer | Rob Landley <rob@landley.net> | 2006-04-18 01:53:41 +0000 |
| commit | a2e98043d5d2e9875cb7b546ec0182893b063f65 (patch) | |
| tree | e8d594fce597d573dc0e4aa2cac6c9123f80e321 /miscutils | |
| parent | 29ba9796be92ccd1250b6c5c3e7906bebed15838 (diff) | |
| download | busybox-w32-a2e98043d5d2e9875cb7b546ec0182893b063f65.tar.gz busybox-w32-a2e98043d5d2e9875cb7b546ec0182893b063f65.tar.bz2 busybox-w32-a2e98043d5d2e9875cb7b546ec0182893b063f65.zip | |
From Rob Sullivan: Fix a segfault with searching, plus some cleanups.
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/less.c | 107 |
1 files changed, 49 insertions, 58 deletions
diff --git a/miscutils/less.c b/miscutils/less.c index 9d42748dc..eee960821 100644 --- a/miscutils/less.c +++ b/miscutils/less.c | |||
| @@ -108,7 +108,6 @@ static int match_lines[100]; | |||
| 108 | static int match_pos; | 108 | static int match_pos; |
| 109 | static int num_matches; | 109 | static int num_matches; |
| 110 | static int match_backwards; | 110 | static int match_backwards; |
| 111 | static int num_back_match = 1; | ||
| 112 | #endif | 111 | #endif |
| 113 | 112 | ||
| 114 | /* Needed termios structures */ | 113 | /* Needed termios structures */ |
| @@ -653,6 +652,16 @@ static char *process_regex_on_line(char *line, regex_t *pattern) | |||
| 653 | return line2; | 652 | return line2; |
| 654 | } | 653 | } |
| 655 | 654 | ||
| 655 | static void goto_match(int match) | ||
| 656 | { | ||
| 657 | /* This goes to a specific match - all line positions of matches are | ||
| 658 | stored within the match_lines[] array. */ | ||
| 659 | if ((match < num_matches) && (match >= 0)) { | ||
| 660 | buffer_line(match_lines[match]); | ||
| 661 | match_pos = match; | ||
| 662 | } | ||
| 663 | } | ||
| 664 | |||
| 656 | static void regex_process(void) | 665 | static void regex_process(void) |
| 657 | { | 666 | { |
| 658 | char uncomp_regex[100]; | 667 | char uncomp_regex[100]; |
| @@ -661,29 +670,29 @@ static void regex_process(void) | |||
| 661 | int j = 0; | 670 | int j = 0; |
| 662 | regex_t pattern; | 671 | regex_t pattern; |
| 663 | 672 | ||
| 664 | /* Reset variables */ | ||
| 665 | match_lines[0] = -1; | ||
| 666 | match_pos = 0; | ||
| 667 | num_matches = 0; | ||
| 668 | match_found = 0; | ||
| 669 | |||
| 670 | /* Get the uncompiled regular expression from the user */ | 673 | /* Get the uncompiled regular expression from the user */ |
| 671 | clear_line(); | 674 | clear_line(); |
| 672 | putchar((match_backwards) ? '?' : '/'); | 675 | putchar((match_backwards) ? '?' : '/'); |
| 673 | uncomp_regex[0] = 0; | 676 | uncomp_regex[0] = 0; |
| 674 | fgets(uncomp_regex, sizeof(uncomp_regex), stdin); | 677 | fgets(uncomp_regex, sizeof(uncomp_regex), inp); |
| 675 | i = strlen(uncomp_regex); | 678 | |
| 676 | if (i > 0) { | 679 | if (strlen(uncomp_regex) == 1) { |
| 677 | if (uncomp_regex[i-1] == '\n') | 680 | goto_match(match_backwards ? match_pos - 1 : match_pos + 1); |
| 678 | uncomp_regex[i-1] = '\0'; | 681 | buffer_print(); |
| 679 | else | ||
| 680 | while((i = getchar()) != '\n' && i != EOF); | ||
| 681 | } else | ||
| 682 | return; | 682 | return; |
| 683 | 683 | } | |
| 684 | |||
| 685 | uncomp_regex[strlen(uncomp_regex) - 1] = '\0'; | ||
| 686 | |||
| 684 | /* Compile the regex and check for errors */ | 687 | /* Compile the regex and check for errors */ |
| 685 | xregcomp(&pattern, uncomp_regex, 0); | 688 | xregcomp(&pattern, uncomp_regex, 0); |
| 686 | 689 | ||
| 690 | /* Reset variables */ | ||
| 691 | match_lines[0] = -1; | ||
| 692 | match_pos = 0; | ||
| 693 | num_matches = 0; | ||
| 694 | match_found = 0; | ||
| 695 | |||
| 687 | /* Run the regex on each line of the current file here */ | 696 | /* Run the regex on each line of the current file here */ |
| 688 | for (i = 0; i <= num_flines; i++) { | 697 | for (i = 0; i <= num_flines; i++) { |
| 689 | strcpy(current_line, process_regex_on_line(flines[i], &pattern)); | 698 | strcpy(current_line, process_regex_on_line(flines[i], &pattern)); |
| @@ -695,41 +704,21 @@ static void regex_process(void) | |||
| 695 | } | 704 | } |
| 696 | 705 | ||
| 697 | num_matches = j; | 706 | num_matches = j; |
| 698 | if ((match_lines[0] != -1) && (num_flines > height - 2)) | 707 | if ((match_lines[0] != -1) && (num_flines > height - 2)) { |
| 699 | buffer_line(match_lines[0]); | 708 | if (match_backwards) { |
| 700 | else | 709 | for (i = 0; i < num_matches; i++) { |
| 701 | buffer_init(); | 710 | if (match_lines[i] > line_pos) { |
| 702 | } | 711 | match_pos = i - 1; |
| 703 | 712 | buffer_line(match_lines[match_pos]); | |
| 704 | static void goto_match(int match) | 713 | break; |
| 705 | { | 714 | } |
| 706 | /* This goes to a specific match - all line positions of matches are | 715 | } |
| 707 | stored within the match_lines[] array. */ | ||
| 708 | if ((match < num_matches) && (match >= 0)) { | ||
| 709 | buffer_line(match_lines[match]); | ||
| 710 | match_pos = match; | ||
| 711 | } | ||
| 712 | } | ||
| 713 | |||
| 714 | static void search_backwards(void) | ||
| 715 | { | ||
| 716 | int current_linepos = line_pos; | ||
| 717 | int i; | ||
| 718 | |||
| 719 | match_backwards = 1; | ||
| 720 | regex_process(); | ||
| 721 | |||
| 722 | for (i = 0; i < num_matches; i++) { | ||
| 723 | if (match_lines[i] > current_linepos) { | ||
| 724 | buffer_line(match_lines[i - num_back_match]); | ||
| 725 | break; | ||
| 726 | } | 716 | } |
| 717 | else | ||
| 718 | buffer_line(match_lines[0]); | ||
| 727 | } | 719 | } |
| 728 | 720 | else | |
| 729 | /* Reset variables */ | 721 | buffer_init(); |
| 730 | match_backwards = 0; | ||
| 731 | num_back_match = 1; | ||
| 732 | |||
| 733 | } | 722 | } |
| 734 | #endif | 723 | #endif |
| 735 | 724 | ||
| @@ -757,8 +746,10 @@ static void number_process(int first_digit) | |||
| 757 | keypress = num_input[i]; | 746 | keypress = num_input[i]; |
| 758 | num_input[i] = '\0'; | 747 | num_input[i] = '\0'; |
| 759 | num = strtol(num_input, &endptr, 10); | 748 | num = strtol(num_input, &endptr, 10); |
| 760 | if (endptr==num_input || *endptr!='\0' || num < 1 || num > MAXLINES) | 749 | if (endptr==num_input || *endptr!='\0' || num < 1 || num > MAXLINES) { |
| 761 | goto END; | 750 | buffer_print(); |
| 751 | return; | ||
| 752 | } | ||
| 762 | 753 | ||
| 763 | /* We now know the number and the letter entered, so we process them */ | 754 | /* We now know the number and the letter entered, so we process them */ |
| 764 | switch (keypress) { | 755 | switch (keypress) { |
| @@ -777,22 +768,20 @@ static void number_process(int first_digit) | |||
| 777 | break; | 768 | break; |
| 778 | #ifdef CONFIG_FEATURE_LESS_REGEXP | 769 | #ifdef CONFIG_FEATURE_LESS_REGEXP |
| 779 | case 'n': | 770 | case 'n': |
| 780 | goto_match(match_pos + num - 1); | 771 | goto_match(match_pos + num); |
| 781 | break; | 772 | break; |
| 782 | case '/': | 773 | case '/': |
| 774 | match_backwards = 0; | ||
| 783 | regex_process(); | 775 | regex_process(); |
| 784 | goto_match(num - 1); | ||
| 785 | break; | 776 | break; |
| 786 | case '?': | 777 | case '?': |
| 787 | num_back_match = num; | 778 | match_backwards = 1; |
| 788 | search_backwards(); | 779 | regex_process(); |
| 789 | break; | 780 | break; |
| 790 | #endif | 781 | #endif |
| 791 | default: | 782 | default: |
| 792 | break; | 783 | break; |
| 793 | } | 784 | } |
| 794 | END: | ||
| 795 | buffer_print(); | ||
| 796 | } | 785 | } |
| 797 | 786 | ||
| 798 | #ifdef CONFIG_FEATURE_LESS_FLAGCS | 787 | #ifdef CONFIG_FEATURE_LESS_FLAGCS |
| @@ -1087,6 +1076,7 @@ static void keypress_process(int keypress) | |||
| 1087 | #endif | 1076 | #endif |
| 1088 | #ifdef CONFIG_FEATURE_LESS_REGEXP | 1077 | #ifdef CONFIG_FEATURE_LESS_REGEXP |
| 1089 | case '/': | 1078 | case '/': |
| 1079 | match_backwards = 0; | ||
| 1090 | regex_process(); | 1080 | regex_process(); |
| 1091 | buffer_print(); | 1081 | buffer_print(); |
| 1092 | break; | 1082 | break; |
| @@ -1099,7 +1089,8 @@ static void keypress_process(int keypress) | |||
| 1099 | buffer_print(); | 1089 | buffer_print(); |
| 1100 | break; | 1090 | break; |
| 1101 | case '?': | 1091 | case '?': |
| 1102 | search_backwards(); | 1092 | match_backwards = 1; |
| 1093 | regex_process(); | ||
| 1103 | buffer_print(); | 1094 | buffer_print(); |
| 1104 | break; | 1095 | break; |
| 1105 | #endif | 1096 | #endif |
