Code

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