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