Skip to content
Newer
Older
100644 209 lines (183 sloc) 6.96 KB
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 12, 2011
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "ignore.h"
7 #include "log.h"
8 #include "options.h"
9 #include "print.h"
10
11 int first_file_match = 1;
12
3a1104f @ggreer Print with colors
authored Dec 12, 2011
13 const char *colors_reset = "\e[0m\e[K";
3898682 @rphillips c89 compatibility and cleanup warnings
rphillips authored Dec 29, 2011
14 const char *colors_path = "\e[1;32m"; /* bold green */
15 const char *colors_match = "\e[30;43m"; /* black with yellow background */
5d5735b @ggreer Print line numbers in color
authored Jan 24, 2012
16 const char *colors_line_number = "\e[1;33m"; /* yellow with black background */
3a1104f @ggreer Print with colors
authored Dec 13, 2011
17
c9f0feb @ggreer Add a separator param to print_path, since I always seem to print a s…
authored Mar 15, 2012
18 void print_path(const char* path, const char sep) {
948235f @ggreer Add some debug statements to check if we're printing the right stuff
authored Mar 10, 2012
19 log_debug("printing path");
20
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
21 if (opts.ackmate) {
c9f0feb @ggreer Add a separator param to print_path, since I always seem to print a s…
authored Mar 15, 2012
22 printf(":%s%c", path, sep);
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
23 }
24 else {
8bb7326 @ggreer Add nocolor option. Don't print colors in ackmate mode
authored Dec 13, 2011
25 if (opts.color) {
c9f0feb @ggreer Add a separator param to print_path, since I always seem to print a s…
authored Mar 15, 2012
26 printf("%s%s%s%c", colors_path, path, colors_reset, sep);
8bb7326 @ggreer Add nocolor option. Don't print colors in ackmate mode
authored Dec 13, 2011
27 }
28 else {
c9f0feb @ggreer Add a separator param to print_path, since I always seem to print a s…
authored Mar 15, 2012
29 printf("%s%c", path, sep);
8bb7326 @ggreer Add nocolor option. Don't print colors in ackmate mode
authored Dec 13, 2011
30 }
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
31 }
32 }
33
6a9bb99 @ggreer Add support for searching all files and ack's unrestricted search.
authored Jan 29, 2012
34 void print_binary_file_matches(const char* path) {
35 print_file_separator();
36 printf("Binary file %s matches.\n", path);
37 }
38
7d6d362 @ggreer And remind myself to fix this.
authored Feb 18, 2012
39 /* TODO: doesn't work for matches across lines */
803458f @ggreer Kill old print function.
authored Dec 24, 2011
40 void print_file_matches(const char* path, const char* buf, const int buf_len, const match matches[], const int matches_len) {
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
41 int line = 1;
883cfaa @ggreer THANKS FOR WARNING ME THAT THIS IS FORBIDDEN, GCC. Apparently I was m…
authored Dec 28, 2011
42 char **context_prev_lines = NULL;
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
43 int prev_line = 0;
44 int last_prev_line = 0;
45 int prev_line_offset = 0;
46 int cur_match = 0;
a5b9b42 @ggreer blah
authored Mar 10, 2012
47 /* TODO the line below contains a terrible hack */
3898682 @rphillips c89 compatibility and cleanup warnings
rphillips authored Dec 29, 2011
48 int lines_since_last_match = 1000000; /* if I initialize this to INT_MAX it'll overflow */
244c054 @ggreer Declare this variable at the start.
authored Mar 16, 2012
49 int lines_to_print = 0;
1ab78b9 @ggreer fuuuuuuuuuuuuuuuuuuuuuuuuuuu
authored Dec 22, 2011
50 int last_printed_match = 0;
11d14cf @ggreer Add support for --nobreak and --noheader. Remove some old comments. S…
authored Dec 26, 2011
51 char sep = '-';
3898682 @rphillips c89 compatibility and cleanup warnings
rphillips authored Dec 29, 2011
52 int i, j;
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
53
cacba8e @ggreer Meh I like this closer to where it's being declared.
authored Dec 26, 2011
54 if (opts.ackmate) {
55 sep = ':';
56 }
57
6a9bb99 @ggreer Add support for searching all files and ack's unrestricted search.
authored Jan 29, 2012
58 print_file_separator();
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
59
b81187f @ggreer Don't print filename before matches if we're only searching one file.
authored Mar 30, 2012
60 if (opts.print_heading == TRUE) {
c9f0feb @ggreer Add a separator param to print_path, since I always seem to print a s…
authored Mar 15, 2012
61 print_path(path, '\n');
11d14cf @ggreer Add support for --nobreak and --noheader. Remove some old comments. S…
authored Dec 26, 2011
62 }
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
63
3855198 @ggreer I forgot about calloc.
authored Mar 16, 2012
64 context_prev_lines = calloc(sizeof(char*), (opts.before + 1));
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
65
fa1ecc4 @ggreer Fix off-by-one error when printing matches at the very end of a file.
authored Mar 19, 2012
66 for (i = 0; i <= buf_len && (cur_match < matches_len || lines_since_last_match <= opts.after); i++) {
02e799d @ggreer Check that cur_match is < matches_len before referencing matches[cur_…
authored Mar 19, 2012
67 if (cur_match < matches_len && i == matches[cur_match].end) {
46cc97f @ggreer Fix bug where we didn't colorize some adjacent matches.
authored Mar 15, 2012
68 /* We found the end of a match. */
2492ce8 @ggreer One less nested control statement. Hooray.
authored Mar 16, 2012
69 cur_match++;
46cc97f @ggreer Fix bug where we didn't colorize some adjacent matches.
authored Mar 15, 2012
70 }
71
7568ec0 @ggreer Dont access uninitialized memory. Thanks, valgrind!
authored Dec 28, 2011
72 if (cur_match < matches_len && i == matches[cur_match].start) {
02c8db0 @ggreer Meh, comments
authored Mar 10, 2012
73 /* We found the start of a match */
43eb7b2 @ggreer Don't print context separator if the context from one match abuts con…
authored Jan 29, 2012
74 if (cur_match > 0 && opts.context && lines_since_last_match > (opts.before + opts.after + 1)) {
1cca629 @ggreer Print those -- lines in context mode. Move my comments closer to wher…
authored Dec 22, 2011
75 printf("--\n");
76 }
77
342616d @ggreer No need to nest these if statements. Just and them.
authored Mar 16, 2012
78 if (lines_since_last_match > 0 && opts.before > 0) {
79 /* TODO: better, but still needs work */
80 /* print the previous line(s) */
244c054 @ggreer Declare this variable at the start.
authored Mar 15, 2012
81 lines_to_print = lines_since_last_match - (opts.after + 1);
342616d @ggreer No need to nest these if statements. Just and them.
authored Mar 15, 2012
82 if (lines_to_print < 0) {
83 lines_to_print = 0;
84 }
85 else if (lines_to_print > opts.before) {
86 lines_to_print = opts.before;
87 }
803a1e3 @ggreer OCD
authored Mar 3, 2012
88
342616d @ggreer No need to nest these if statements. Just and them.
authored Mar 15, 2012
89 for (j = (opts.before - lines_to_print); j < opts.before; j++) {
90 prev_line = (last_prev_line + j) % opts.before;
91 if (context_prev_lines[prev_line] != NULL) {
92 if (opts.print_heading == 0) {
93 print_path(path, ':');
cadc76d @ggreer Print context before
authored Dec 22, 2011
94 }
342616d @ggreer No need to nest these if statements. Just and them.
authored Mar 15, 2012
95 print_line_number(line - (opts.before - j), sep);
96 printf("%s\n", context_prev_lines[prev_line]);
cadc76d @ggreer Print context before
authored Dec 22, 2011
97 }
98 }
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
99 }
100 lines_since_last_match = 0;
101 }
102
02c8db0 @ggreer Meh, comments
authored Mar 10, 2012
103 /* We found the end of a line. */
d0a90ba @ggreer Use and instead of nesting ifs.
authored Mar 16, 2012
104 if (buf[i] == '\n' && opts.before > 0) {
105 if (context_prev_lines[last_prev_line] != NULL) {
106 free(context_prev_lines[last_prev_line]);
8e96870 @ggreer Kinda start of actually printing context
authored Dec 22, 2011
107 }
8922d47 @ggreer This comment was stale. Whoops.
authored Mar 16, 2012
108 /* We don't want to strcpy the \n */
d0a90ba @ggreer Use and instead of nesting ifs.
authored Mar 15, 2012
109 context_prev_lines[last_prev_line] = strndup(&buf[prev_line_offset], i - prev_line_offset);
110 last_prev_line = (last_prev_line + 1) % opts.before;
8e96870 @ggreer Kinda start of actually printing context
authored Dec 22, 2011
111 }
112
fa1ecc4 @ggreer Fix off-by-one error when printing matches at the very end of a file.
authored Mar 19, 2012
113 if (buf[i] == '\n' || i == buf_len) {
87d1df8 @ggreer Worst commit message ever.
authored Dec 26, 2011
114 if (lines_since_last_match == 0) {
b0b09ab @ggreer Rename opts.search_stdin to opts.search_stream.
authored Apr 9, 2012
115 if (opts.print_heading == 0 && !opts.search_stream) {
b4dd2ac @ggreer Completely redo how we print the matching line. I think this is a lot…
authored Mar 16, 2012
116 print_path(path, ':');
117 }
118
87d1df8 @ggreer Worst commit message ever.
authored Dec 27, 2011
119 if (opts.ackmate) {
3898682 @rphillips c89 compatibility and cleanup warnings
rphillips authored Dec 29, 2011
120 /* print headers for ackmate to parse */
ecf7245 @ggreer Start moving line number printing into its own function. print_file_m…
authored Mar 10, 2012
121 print_line_number(line, ';');
9d11c0a @ggreer Turn this while into a for
authored Mar 16, 2012
122 for (; last_printed_match < cur_match; last_printed_match++) {
f5ff4c1 @ggreer This line was kinda long.
authored Mar 16, 2012
123 printf("%i %i",
124 (matches[last_printed_match].start - prev_line_offset),
125 (matches[last_printed_match].end - matches[last_printed_match].start)
126 );
2cb32d9 @ggreer Ternary operator awww yeahhh
authored Mar 16, 2012
127 last_printed_match == cur_match - 1 ? putchar(':') : putchar(',');
87d1df8 @ggreer Worst commit message ever.
authored Dec 27, 2011
128 }
b4dd2ac @ggreer Completely redo how we print the matching line. I think this is a lot…
authored Mar 15, 2012
129 j = prev_line_offset;
130 }
131 else {
132 print_line_number(line, ':');
133 if (opts.column) {
f3e7586 @ggreer Whoops, column was wrong here.
authored Mar 16, 2012
134 printf("%i:", (matches[last_printed_match].start - prev_line_offset) + 1);
b4dd2ac @ggreer Completely redo how we print the matching line. I think this is a lot…
authored Mar 15, 2012
135 }
136
137 for (j = prev_line_offset; j < matches[last_printed_match].start; j++) {
f344c7c @ggreer Whoops
authored Dec 26, 2011
138 putchar(buf[j]);
139 }
b4dd2ac @ggreer Completely redo how we print the matching line. I think this is a lot…
authored Mar 15, 2012
140 for (; last_printed_match < cur_match; last_printed_match++) {
141 for (; j < matches[last_printed_match].start; j++) {
142 putchar(buf[j]);
143 }
144 if (opts.color) {
145 printf("%s", colors_match);
146 }
147 for (; j < matches[last_printed_match].end; j++) {
148 putchar(buf[j]);
149 }
150 if (opts.color) {
151 printf("%s", colors_reset);
152 }
153 }
154 }
ab1597e @ggreer Whoops that's a bad idea.
authored Mar 16, 2012
155 /* print up to current char */
156 for (; j < i; j++) {
157 putchar(buf[j]);
158 }
159 putchar('\n');
f344c7c @ggreer Whoops
authored Dec 27, 2011
160 }
8f95db6 @ggreer BOOYAH
authored Dec 26, 2011
161 else if (lines_since_last_match <= opts.after) {
3898682 @rphillips c89 compatibility and cleanup warnings
rphillips authored Dec 29, 2011
162 /* print context after matching line */
8f95db6 @ggreer BOOYAH
authored Dec 27, 2011
163 if (opts.print_heading == 0) {
c9f0feb @ggreer Add a separator param to print_path, since I always seem to print a s…
authored Mar 15, 2012
164 print_path(path, ':');
8f95db6 @ggreer BOOYAH
authored Dec 27, 2011
165 }
791baeb @ggreer Found another place where I'm printing the line number.
authored Mar 10, 2012
166 print_line_number(line, sep);
8f95db6 @ggreer BOOYAH
authored Dec 27, 2011
167
ab1597e @ggreer Whoops that's a bad idea.
authored Mar 15, 2012
168 for (j = prev_line_offset; j < i; j++) {
169 putchar(buf[j]);
170 }
171 putchar('\n');
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
172 }
173
3898682 @rphillips c89 compatibility and cleanup warnings
rphillips authored Dec 29, 2011
174 prev_line_offset = i + 1; /* skip the newline */
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
175 line++;
176 lines_since_last_match++;
177 }
178 }
883cfaa @ggreer THANKS FOR WARNING ME THAT THIS IS FORBIDDEN, GCC. Apparently I was m…
authored Dec 28, 2011
179
5c5a80c @ggreer Fix a memory leak when printing context. I don't know why valgrind di…
authored Mar 16, 2012
180 for (i = 0; i < opts.before; i++) {
181 if (context_prev_lines[i] != NULL) {
182 free(context_prev_lines[i]);
183 }
184 }
883cfaa @ggreer THANKS FOR WARNING ME THAT THIS IS FORBIDDEN, GCC. Apparently I was m…
authored Dec 28, 2011
185 free(context_prev_lines);
bf7d1e6 @ggreer Move printing stuff into its own source files
authored Dec 13, 2011
186 }
6a9bb99 @ggreer Add support for searching all files and ack's unrestricted search.
authored Jan 29, 2012
187
38124cc @ggreer Make these function params consts.
authored Mar 15, 2012
188 void print_line_number(const int line, const char sep) {
a2bbca6 @ggreer Don't print line numbers when printing stdin. This fixes another prin…
authored Mar 15, 2012
189 if (!opts.print_line_numbers) {
190 return;
191 }
ecf7245 @ggreer Start moving line number printing into its own function. print_file_m…
authored Mar 10, 2012
192 log_debug("printing line number");
a2bbca6 @ggreer Don't print line numbers when printing stdin. This fixes another prin…
authored Mar 15, 2012
193
3c76c31 @ggreer Simplify
authored Mar 16, 2012
194 if (opts.color) {
195 printf("%s%i%s%c", colors_line_number, line, colors_reset, sep);
ecf7245 @ggreer Start moving line number printing into its own function. print_file_m…
authored Mar 10, 2012
196 }
197 else {
3c76c31 @ggreer Simplify
authored Mar 15, 2012
198 printf("%i%c", line, sep);
ecf7245 @ggreer Start moving line number printing into its own function. print_file_m…
authored Mar 10, 2012
199 }
200 }
201
6a9bb99 @ggreer Add support for searching all files and ack's unrestricted search.
authored Jan 29, 2012
202 void print_file_separator() {
203 if (first_file_match == 0 && opts.print_break) {
5bd9636 @ggreer Only log that we're printing the file separator if we're actually pri…
authored Mar 15, 2012
204 log_debug("printing file separator");
6a9bb99 @ggreer Add support for searching all files and ack's unrestricted search.
authored Jan 29, 2012
205 printf("\n");
206 }
207 first_file_match = 0;
208 }
Something went wrong with that request. Please try again.