1 --- busybox/miscutils/less.c 2006-04-22 08:20:29.000000000 +0100
2 +++ busybox/miscutils/less.c 2006-04-23 11:28:08.000000000 +0100
5 #ifdef CONFIG_FEATURE_LESS_REGEXP
6 static int match_found;
7 -static int match_lines[100];
8 +static int *match_lines;
10 static int num_matches;
11 static int match_backwards;
12 +static regex_t old_pattern;
15 /* Needed termios structures */
20 -/* Turn a percentage into a line number */
21 -static int reverse_percent(int percentage)
23 - double linenum = percentage;
24 - linenum = ((linenum / 100) * num_flines) - 1;
28 #ifdef CONFIG_FEATURE_LESS_FLAGS
30 /* Interestingly, writing calc_percent as a function and not a prototype saves around 32 bytes
32 static void buffer_line(int linenum)
38 - if (linenum < 1 || linenum > num_flines) {
39 + if (linenum < 0 || linenum > num_flines) {
41 - printf("%s%s%i%s", HIGHLIGHT, "Cannot seek to line number ", linenum, NORMAL);
42 + printf("%s%s%i%s", HIGHLIGHT, "Cannot seek to line number ", linenum + 1, NORMAL);
44 else if (linenum < (num_flines - height - 2)) {
45 for (i = 0; i < (height - 1); i++) {
47 buffer[i] = bb_xstrdup(flines[linenum + i]);
53 for (i = 0; i < (height - 1); i++) {
56 /* Set past_eof so buffer_down and buffer_up act differently */
63 /* Get a regular expression from the user, and then go through the current
64 file line by line, running a processing regex function on each one. */
66 -static char *insert_highlights(char *line, int start, int end)
68 - return bb_xasprintf("%.*s%s%.*s%s%s", start, line, HIGHLIGHT,
69 - end - start, line + start, NORMAL, line + end);
72 -static char *process_regex_on_line(char *line, regex_t *pattern)
73 +static char *process_regex_on_line(char *line, regex_t *pattern, int action)
75 /* This function takes the regex and applies it to the line.
76 Each part of the line that matches has the HIGHLIGHT
77 and NORMAL escape sequences placed around it by
78 - insert_highlights, and then the line is returned. */
80 + insert_highlights if action = 1, or has the escape sequences
81 + removed if action = 0, and then the line is returned. */
83 char *line2 = (char *) xmalloc((sizeof(char) * (strlen(line) + 1)) + 64);
86 + char *growline = "";
87 regmatch_t match_structs;
89 - strcpy(line2, line);
90 + line2 = bb_xstrdup(line);
93 match_status = regexec(pattern, line2, 1, &match_structs, 0);
96 while (match_status == 0) {
98 - memset(sub_line, 0, sizeof(sub_line));
100 if (match_found == 0)
103 - line2 = insert_highlights(line2, match_structs.rm_so + prev_eo, match_structs.rm_eo + prev_eo);
104 - if ((size_t)match_structs.rm_eo + 11 + prev_eo < strlen(line2))
105 - strcat(sub_line, line2 + match_structs.rm_eo + 11 + prev_eo);
107 - prev_eo += match_structs.rm_eo + 11;
108 - match_status = regexec(pattern, sub_line, 1, &match_structs, REG_NOTBOL);
111 + growline = bb_xasprintf("%s%.*s%s%.*s%s", growline, match_structs.rm_so, line2, HIGHLIGHT, match_structs.rm_eo - match_structs.rm_so, line2 + match_structs.rm_so, NORMAL);
114 + growline = bb_xasprintf("%s%.*s%.*s", growline, match_structs.rm_so - 4, line2, match_structs.rm_eo - match_structs.rm_so, line2 + match_structs.rm_so);
117 + line2 += match_structs.rm_eo;
118 + match_status = regexec(pattern, line2, 1, &match_structs, REG_NOTBOL);
123 + growline = bb_xasprintf("%s%s", growline, line2);
125 + return (match_found ? growline : line);
131 static void goto_match(int match)
132 @@ -665,11 +657,10 @@
133 static void regex_process(void)
135 char uncomp_regex[100];
136 - char current_line[256];
137 + char *current_line;
142 /* Get the uncompiled regular expression from the user */
144 putchar((match_backwards) ? '?' : '/');
145 @@ -677,32 +668,43 @@
146 fgets(uncomp_regex, sizeof(uncomp_regex), inp);
148 if (strlen(uncomp_regex) == 1) {
149 - goto_match(match_backwards ? match_pos - 1 : match_pos + 1);
152 + goto_match(match_backwards ? match_pos - 1 : match_pos + 1);
158 uncomp_regex[strlen(uncomp_regex) - 1] = '\0';
160 /* Compile the regex and check for errors */
161 xregcomp(&pattern, uncomp_regex, 0);
164 + /* Get rid of all the highlights we added previously */
165 + for (i = 0; i <= num_flines; i++) {
166 + current_line = process_regex_on_line(flines[i], &old_pattern, 0);
167 + flines[i] = bb_xstrdup(current_line);
170 + old_pattern = pattern;
172 /* Reset variables */
173 + match_lines = xrealloc(match_lines, sizeof(int));
179 /* Run the regex on each line of the current file here */
180 for (i = 0; i <= num_flines; i++) {
181 - strcpy(current_line, process_regex_on_line(flines[i], &pattern));
182 + current_line = process_regex_on_line(flines[i], &pattern, 1);
183 flines[i] = bb_xstrdup(current_line);
185 + match_lines = xrealloc(match_lines, (j + 1) * sizeof(int));
193 if ((match_lines[0] != -1) && (num_flines > height - 2)) {
194 if (match_backwards) {
196 buffer_line(num - 1);
199 - buffer_line(reverse_percent(num));
200 + buffer_line(((num / 100) * num_flines) - 1);
202 #ifdef CONFIG_FEATURE_LESS_REGEXP
207 buffer_line(temp_line_pos);
213 printf("Log file: ");
214 fgets(current_line, 256, inp);
215 current_line[strlen(current_line) - 1] = '\0';
216 - if (strlen(current_line)) {
217 + if (strlen(current_line) > 1) {
218 fp = bb_xfopen(current_line, "w");
219 for (i = 0; i < num_flines; i++)
220 fprintf(fp, "%s", flines[i]);
222 printf("%s%s%s", HIGHLIGHT, "No matching bracket found", NORMAL);
224 buffer_line(bracket_line - height + 2);
229 @@ -1001,7 +1001,6 @@
230 printf("%s%s%s", HIGHLIGHT, "No matching bracket found", NORMAL);
232 buffer_line(bracket_line);
237 @@ -1035,12 +1034,10 @@
240 case 'g': case 'p': case '<': case '%':
241 - buffer_up(num_flines + 1);
246 - buffer_down(num_flines + 1);
248 + buffer_line(num_flines - height + 2);
252 @@ -1078,20 +1075,16 @@
259 goto_match(match_pos + 1);
263 goto_match(match_pos - 1);
272 #ifdef CONFIG_FEATURE_LESS_FLAGCS