1/*
2 * Whitespace rules
3 *
4 * Copyright (c) 2007 Junio C Hamano
5 */
6
7#include "cache.h"
8#include "attr.h"
9
10static struct whitespace_rule {
11 const char *rule_name;
12 unsigned rule_bits;
13 unsigned loosens_error;
14} whitespace_rule_names[] = {
15 { "trailing-space", WS_TRAILING_SPACE, 0 },
16 { "space-before-tab", WS_SPACE_BEFORE_TAB, 0 },
17 { "indent-with-non-tab", WS_INDENT_WITH_NON_TAB, 0 },
18 { "cr-at-eol", WS_CR_AT_EOL, 1 },
19};
20
21unsigned parse_whitespace_rule(const char *string)
22{
23 unsigned rule = WS_DEFAULT_RULE;
24
25 while (string) {
26 int i;
27 size_t len;
28 const char *ep;
29 int negated = 0;
30
31 string = string + strspn(string, ", \t\n\r");
32 ep = strchr(string, ',');
33 if (!ep)
34 len = strlen(string);
35 else
36 len = ep - string;
37
38 if (*string == '-') {
39 negated = 1;
40 string++;
41 len--;
42 }
43 if (!len)
44 break;
45 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) {
46 if (strncmp(whitespace_rule_names[i].rule_name,
47 string, len))
48 continue;
49 if (negated)
50 rule &= ~whitespace_rule_names[i].rule_bits;
51 else
52 rule |= whitespace_rule_names[i].rule_bits;
53 break;
54 }
55 string = ep;
56 }
57 return rule;
58}
59
60static void setup_whitespace_attr_check(struct git_attr_check *check)
61{
62 static struct git_attr *attr_whitespace;
63
64 if (!attr_whitespace)
65 attr_whitespace = git_attr("whitespace", 10);
66 check[0].attr = attr_whitespace;
67}
68
69unsigned whitespace_rule(const char *pathname)
70{
71 struct git_attr_check attr_whitespace_rule;
72
73 setup_whitespace_attr_check(&attr_whitespace_rule);
74 if (!git_checkattr(pathname, 1, &attr_whitespace_rule)) {
75 const char *value;
76
77 value = attr_whitespace_rule.value;
78 if (ATTR_TRUE(value)) {
79 /* true (whitespace) */
80 unsigned all_rule = 0;
81 int i;
82 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
83 if (!whitespace_rule_names[i].loosens_error)
84 all_rule |= whitespace_rule_names[i].rule_bits;
85 return all_rule;
86 } else if (ATTR_FALSE(value)) {
87 /* false (-whitespace) */
88 return 0;
89 } else if (ATTR_UNSET(value)) {
90 /* reset to default (!whitespace) */
91 return whitespace_rule_cfg;
92 } else {
93 /* string */
94 return parse_whitespace_rule(value);
95 }
96 } else {
97 return whitespace_rule_cfg;
98 }
99}
100
101/* The returned string should be freed by the caller. */
102char *whitespace_error_string(unsigned ws)
103{
104 struct strbuf err = STRBUF_INIT;
105 if (ws & WS_TRAILING_SPACE)
106 strbuf_addstr(&err, "trailing whitespace");
107 if (ws & WS_SPACE_BEFORE_TAB) {
108 if (err.len)
109 strbuf_addstr(&err, ", ");
110 strbuf_addstr(&err, "space before tab in indent");
111 }
112 if (ws & WS_INDENT_WITH_NON_TAB) {
113 if (err.len)
114 strbuf_addstr(&err, ", ");
115 strbuf_addstr(&err, "indent with spaces");
116 }
117 return strbuf_detach(&err, NULL);
118}
119
120/* If stream is non-NULL, emits the line after checking. */
121static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
122 FILE *stream, const char *set,
123 const char *reset, const char *ws)
124{
125 unsigned result = 0;
126 int written = 0;
127 int trailing_whitespace = -1;
128 int trailing_newline = 0;
129 int trailing_carriage_return = 0;
130 int i;
131
132 /* Logic is simpler if we temporarily ignore the trailing newline. */
133 if (len > 0 && line[len - 1] == '\n') {
134 trailing_newline = 1;
135 len--;
136 }
137 if ((ws_rule & WS_CR_AT_EOL) &&
138 len > 0 && line[len - 1] == '\r') {
139 trailing_carriage_return = 1;
140 len--;
141 }
142
143 /* Check for trailing whitespace. */
144 if (ws_rule & WS_TRAILING_SPACE) {
145 for (i = len - 1; i >= 0; i--) {
146 if (isspace(line[i])) {
147 trailing_whitespace = i;
148 result |= WS_TRAILING_SPACE;
149 }
150 else
151 break;
152 }
153 }
154
155 /* Check for space before tab in initial indent. */
156 for (i = 0; i < len; i++) {
157 if (line[i] == ' ')
158 continue;
159 if (line[i] != '\t')
160 break;
161 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
162 result |= WS_SPACE_BEFORE_TAB;
163 if (stream) {
164 fputs(ws, stream);
165 fwrite(line + written, i - written, 1, stream);
166 fputs(reset, stream);
167 }
168 } else if (stream)
169 fwrite(line + written, i - written, 1, stream);
170 if (stream)
171 fwrite(line + i, 1, 1, stream);
172 written = i + 1;
173 }
174
175 /* Check for indent using non-tab. */
176 if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= 8) {
177 result |= WS_INDENT_WITH_NON_TAB;
178 if (stream) {
179 fputs(ws, stream);
180 fwrite(line + written, i - written, 1, stream);
181 fputs(reset, stream);
182 }
183 written = i;
184 }
185
186 if (stream) {
187 /*
188 * Now the rest of the line starts at "written".
189 * The non-highlighted part ends at "trailing_whitespace".
190 */
191 if (trailing_whitespace == -1)
192 trailing_whitespace = len;
193
194 /* Emit non-highlighted (middle) segment. */
195 if (trailing_whitespace - written > 0) {
196 fputs(set, stream);
197 fwrite(line + written,
198 trailing_whitespace - written, 1, stream);
199 fputs(reset, stream);
200 }
201
202 /* Highlight errors in trailing whitespace. */
203 if (trailing_whitespace != len) {
204 fputs(ws, stream);
205 fwrite(line + trailing_whitespace,
206 len - trailing_whitespace, 1, stream);
207 fputs(reset, stream);
208 }
209 if (trailing_carriage_return)
210 fputc('\r', stream);
211 if (trailing_newline)
212 fputc('\n', stream);
213 }
214 return result;
215}
216
217void ws_check_emit(const char *line, int len, unsigned ws_rule,
218 FILE *stream, const char *set,
219 const char *reset, const char *ws)
220{
221 (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws);
222}
223
224unsigned ws_check(const char *line, int len, unsigned ws_rule)
225{
226 return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
227}
228
229int ws_blank_line(const char *line, int len, unsigned ws_rule)
230{
231 /*
232 * We _might_ want to treat CR differently from other
233 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
234 * for now we just use this stupid definition.
235 */
236 while (len-- > 0) {
237 if (!isspace(*line))
238 return 0;
239 line++;
240 }
241 return 1;
242}
243
244/* Copy the line to the buffer while fixing whitespaces */
245int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *error_count)
246{
247 /*
248 * len is number of bytes to be copied from src, starting
249 * at src. Typically src[len-1] is '\n', unless this is
250 * the incomplete last line.
251 */
252 int i;
253 int add_nl_to_tail = 0;
254 int add_cr_to_tail = 0;
255 int fixed = 0;
256 int last_tab_in_indent = -1;
257 int last_space_in_indent = -1;
258 int need_fix_leading_space = 0;
259 char *buf;
260
261 /*
262 * Strip trailing whitespace
263 */
264 if ((ws_rule & WS_TRAILING_SPACE) &&
265 (2 <= len && isspace(src[len-2]))) {
266 if (src[len - 1] == '\n') {
267 add_nl_to_tail = 1;
268 len--;
269 if (1 < len && src[len - 1] == '\r') {
270 add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL);
271 len--;
272 }
273 }
274 if (0 < len && isspace(src[len - 1])) {
275 while (0 < len && isspace(src[len-1]))
276 len--;
277 fixed = 1;
278 }
279 }
280
281 /*
282 * Check leading whitespaces (indent)
283 */
284 for (i = 0; i < len; i++) {
285 char ch = src[i];
286 if (ch == '\t') {
287 last_tab_in_indent = i;
288 if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
289 0 <= last_space_in_indent)
290 need_fix_leading_space = 1;
291 } else if (ch == ' ') {
292 last_space_in_indent = i;
293 if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
294 8 <= i - last_tab_in_indent)
295 need_fix_leading_space = 1;
296 } else
297 break;
298 }
299
300 buf = dst;
301 if (need_fix_leading_space) {
302 /* Process indent ourselves */
303 int consecutive_spaces = 0;
304 int last = last_tab_in_indent + 1;
305
306 if (ws_rule & WS_INDENT_WITH_NON_TAB) {
307 /* have "last" point at one past the indent */
308 if (last_tab_in_indent < last_space_in_indent)
309 last = last_space_in_indent + 1;
310 else
311 last = last_tab_in_indent + 1;
312 }
313
314 /*
315 * between src[0..last-1], strip the funny spaces,
316 * updating them to tab as needed.
317 */
318 for (i = 0; i < last; i++) {
319 char ch = src[i];
320 if (ch != ' ') {
321 consecutive_spaces = 0;
322 *dst++ = ch;
323 } else {
324 consecutive_spaces++;
325 if (consecutive_spaces == 8) {
326 *dst++ = '\t';
327 consecutive_spaces = 0;
328 }
329 }
330 }
331 while (0 < consecutive_spaces--)
332 *dst++ = ' ';
333 len -= last;
334 src += last;
335 fixed = 1;
336 }
337
338 memcpy(dst, src, len);
339 if (add_cr_to_tail)
340 dst[len++] = '\r';
341 if (add_nl_to_tail)
342 dst[len++] = '\n';
343 if (fixed && error_count)
344 (*error_count)++;
345 return dst + len - buf;
346}