Code

d56636be7b3f28b589da3692e31a920a57653e74
[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         { "blank-at-eof", WS_BLANK_AT_EOF },
19 };
21 unsigned parse_whitespace_rule(const char *string)
22 {
23         unsigned rule = WS_DEFAULT_RULE;
25         while (string) {
26                 int i;
27                 size_t len;
28                 const char *ep;
29                 int negated = 0;
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;
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 }
60 static void setup_whitespace_attr_check(struct git_attr_check *check)
61 {
62         static struct git_attr *attr_whitespace;
64         if (!attr_whitespace)
65                 attr_whitespace = git_attr("whitespace", 10);
66         check[0].attr = attr_whitespace;
67 }
69 unsigned whitespace_rule(const char *pathname)
70 {
71         struct git_attr_check attr_whitespace_rule;
73         setup_whitespace_attr_check(&attr_whitespace_rule);
74         if (!git_checkattr(pathname, 1, &attr_whitespace_rule)) {
75                 const char *value;
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                                 all_rule |= whitespace_rule_names[i].rule_bits;
84                         return all_rule;
85                 } else if (ATTR_FALSE(value)) {
86                         /* false (-whitespace) */
87                         return 0;
88                 } else if (ATTR_UNSET(value)) {
89                         /* reset to default (!whitespace) */
90                         return whitespace_rule_cfg;
91                 } else {
92                         /* string */
93                         return parse_whitespace_rule(value);
94                 }
95         } else {
96                 return whitespace_rule_cfg;
97         }
98 }
100 /* The returned string should be freed by the caller. */
101 char *whitespace_error_string(unsigned ws)
103         struct strbuf err;
104         strbuf_init(&err, 0);
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         if (ws & WS_BLANK_AT_EOF) {
118                 if (err.len)
119                         strbuf_addstr(&err, ", ");
120                 strbuf_addstr(&err, "new blank line at EOF");
121         }
122         return strbuf_detach(&err, NULL);
125 /* If stream is non-NULL, emits the line after checking. */
126 static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
127                                 FILE *stream, const char *set,
128                                 const char *reset, const char *ws)
130         unsigned result = 0;
131         int written = 0;
132         int trailing_whitespace = -1;
133         int trailing_newline = 0;
134         int trailing_carriage_return = 0;
135         int i;
137         /* Logic is simpler if we temporarily ignore the trailing newline. */
138         if (len > 0 && line[len - 1] == '\n') {
139                 trailing_newline = 1;
140                 len--;
141         }
142         if ((ws_rule & WS_CR_AT_EOL) &&
143             len > 0 && line[len - 1] == '\r') {
144                 trailing_carriage_return = 1;
145                 len--;
146         }
148         /* Check for trailing whitespace. */
149         if (ws_rule & WS_TRAILING_SPACE) {
150                 for (i = len - 1; i >= 0; i--) {
151                         if (isspace(line[i])) {
152                                 trailing_whitespace = i;
153                                 result |= WS_TRAILING_SPACE;
154                         }
155                         else
156                                 break;
157                 }
158         }
160         /* Check for space before tab in initial indent. */
161         for (i = 0; i < len; i++) {
162                 if (line[i] == ' ')
163                         continue;
164                 if (line[i] != '\t')
165                         break;
166                 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
167                         result |= WS_SPACE_BEFORE_TAB;
168                         if (stream) {
169                                 fputs(ws, stream);
170                                 fwrite(line + written, i - written, 1, stream);
171                                 fputs(reset, stream);
172                         }
173                 } else if (stream)
174                         fwrite(line + written, i - written, 1, stream);
175                 if (stream)
176                         fwrite(line + i, 1, 1, stream);
177                 written = i + 1;
178         }
180         /* Check for indent using non-tab. */
181         if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= 8) {
182                 result |= WS_INDENT_WITH_NON_TAB;
183                 if (stream) {
184                         fputs(ws, stream);
185                         fwrite(line + written, i - written, 1, stream);
186                         fputs(reset, stream);
187                 }
188                 written = i;
189         }
191         if (stream) {
192                 /*
193                  * Now the rest of the line starts at "written".
194                  * The non-highlighted part ends at "trailing_whitespace".
195                  */
196                 if (trailing_whitespace == -1)
197                         trailing_whitespace = len;
199                 /* Emit non-highlighted (middle) segment. */
200                 if (trailing_whitespace - written > 0) {
201                         fputs(set, stream);
202                         fwrite(line + written,
203                             trailing_whitespace - written, 1, stream);
204                         fputs(reset, stream);
205                 }
207                 /* Highlight errors in trailing whitespace. */
208                 if (trailing_whitespace != len) {
209                         fputs(ws, stream);
210                         fwrite(line + trailing_whitespace,
211                             len - trailing_whitespace, 1, stream);
212                         fputs(reset, stream);
213                 }
214                 if (trailing_carriage_return)
215                         fputc('\r', stream);
216                 if (trailing_newline)
217                         fputc('\n', stream);
218         }
219         return result;
222 void ws_check_emit(const char *line, int len, unsigned ws_rule,
223                    FILE *stream, const char *set,
224                    const char *reset, const char *ws)
226         (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws);
229 unsigned ws_check(const char *line, int len, unsigned ws_rule)
231         return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
234 int ws_blank_line(const char *line, int len, unsigned ws_rule)
236         /*
237          * We _might_ want to treat CR differently from other
238          * whitespace characters when ws_rule has WS_CR_AT_EOL, but
239          * for now we just use this stupid definition.
240          */
241         while (len-- > 0) {
242                 if (!isspace(*line))
243                         return 0;
244                 line++;
245         }
246         return 1;
249 /* Copy the line to the buffer while fixing whitespaces */
250 int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *error_count)
252         /*
253          * len is number of bytes to be copied from src, starting
254          * at src.  Typically src[len-1] is '\n', unless this is
255          * the incomplete last line.
256          */
257         int i;
258         int add_nl_to_tail = 0;
259         int add_cr_to_tail = 0;
260         int fixed = 0;
261         int last_tab_in_indent = -1;
262         int last_space_in_indent = -1;
263         int need_fix_leading_space = 0;
264         char *buf;
266         /*
267          * Strip trailing whitespace
268          */
269         if ((ws_rule & WS_TRAILING_SPACE) &&
270             (2 <= len && isspace(src[len-2]))) {
271                 if (src[len - 1] == '\n') {
272                         add_nl_to_tail = 1;
273                         len--;
274                         if (1 < len && src[len - 1] == '\r') {
275                                 add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL);
276                                 len--;
277                         }
278                 }
279                 if (0 < len && isspace(src[len - 1])) {
280                         while (0 < len && isspace(src[len-1]))
281                                 len--;
282                         fixed = 1;
283                 }
284         }
286         /*
287          * Check leading whitespaces (indent)
288          */
289         for (i = 0; i < len; i++) {
290                 char ch = src[i];
291                 if (ch == '\t') {
292                         last_tab_in_indent = i;
293                         if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
294                             0 <= last_space_in_indent)
295                             need_fix_leading_space = 1;
296                 } else if (ch == ' ') {
297                         last_space_in_indent = i;
298                         if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
299                             8 <= i - last_tab_in_indent)
300                                 need_fix_leading_space = 1;
301                 } else
302                         break;
303         }
305         buf = dst;
306         if (need_fix_leading_space) {
307                 /* Process indent ourselves */
308                 int consecutive_spaces = 0;
309                 int last = last_tab_in_indent + 1;
311                 if (ws_rule & WS_INDENT_WITH_NON_TAB) {
312                         /* have "last" point at one past the indent */
313                         if (last_tab_in_indent < last_space_in_indent)
314                                 last = last_space_in_indent + 1;
315                         else
316                                 last = last_tab_in_indent + 1;
317                 }
319                 /*
320                  * between src[0..last-1], strip the funny spaces,
321                  * updating them to tab as needed.
322                  */
323                 for (i = 0; i < last; i++) {
324                         char ch = src[i];
325                         if (ch != ' ') {
326                                 consecutive_spaces = 0;
327                                 *dst++ = ch;
328                         } else {
329                                 consecutive_spaces++;
330                                 if (consecutive_spaces == 8) {
331                                         *dst++ = '\t';
332                                         consecutive_spaces = 0;
333                                 }
334                         }
335                 }
336                 while (0 < consecutive_spaces--)
337                         *dst++ = ' ';
338                 len -= last;
339                 src += last;
340                 fixed = 1;
341         }
343         memcpy(dst, src, len);
344         if (add_cr_to_tail)
345                 dst[len++] = '\r';
346         if (add_nl_to_tail)
347                 dst[len++] = '\n';
348         if (fixed && error_count)
349                 (*error_count)++;
350         return dst + len - buf;