Lukas Sandström | 7499c99 | 2006-06-13 22:21:53 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Carlos Rica | 9690c11 | 2007-06-25 21:28:01 +0200 | [diff] [blame] | 2 | #include "cache.h" |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 3 | |
| 4 | /* |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 5 | * Returns the length of a line, without trailing spaces. |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 6 | * |
Carlos Rica | 9690c11 | 2007-06-25 21:28:01 +0200 | [diff] [blame] | 7 | * If the line ends with newline, it will be removed too. |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 8 | */ |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 9 | static size_t cleanup(char *line, size_t len) |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 10 | { |
Kristian Høgsberg | 6d69b6f | 2007-09-17 20:06:45 -0400 | [diff] [blame] | 11 | while (len) { |
| 12 | unsigned char c = line[len - 1]; |
| 13 | if (!isspace(c)) |
| 14 | break; |
| 15 | len--; |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 16 | } |
Kristian Høgsberg | 6d69b6f | 2007-09-17 20:06:45 -0400 | [diff] [blame] | 17 | |
Carlos Rica | 9690c11 | 2007-06-25 21:28:01 +0200 | [diff] [blame] | 18 | return len; |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 19 | } |
| 20 | |
Carlos Rica | 9690c11 | 2007-06-25 21:28:01 +0200 | [diff] [blame] | 21 | /* |
| 22 | * Remove empty lines from the beginning and end |
| 23 | * and also trailing spaces from every line. |
| 24 | * |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 25 | * Note that the buffer will not be NUL-terminated. |
| 26 | * |
Carlos Rica | 9690c11 | 2007-06-25 21:28:01 +0200 | [diff] [blame] | 27 | * Turn multiple consecutive empty lines between paragraphs |
| 28 | * into just one empty line. |
| 29 | * |
| 30 | * If the input has only empty lines and spaces, |
| 31 | * no output will be produced. |
| 32 | * |
Kristian Høgsberg | 6d69b6f | 2007-09-17 20:06:45 -0400 | [diff] [blame] | 33 | * If last line does not have a newline at the end, one is added. |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 34 | * |
Carlos Rica | 9690c11 | 2007-06-25 21:28:01 +0200 | [diff] [blame] | 35 | * Enable skip_comments to skip every line starting with "#". |
| 36 | */ |
Kristian Høgsberg | 6d69b6f | 2007-09-17 20:06:45 -0400 | [diff] [blame] | 37 | void stripspace(struct strbuf *sb, int skip_comments) |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 38 | { |
Kristian Høgsberg | 6d69b6f | 2007-09-17 20:06:45 -0400 | [diff] [blame] | 39 | int empties = 0; |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 40 | size_t i, j, len, newlen; |
| 41 | char *eol; |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 42 | |
Kristian Høgsberg | 6d69b6f | 2007-09-17 20:06:45 -0400 | [diff] [blame] | 43 | /* We may have to add a newline. */ |
| 44 | strbuf_grow(sb, 1); |
Carlos Rica | 9690c11 | 2007-06-25 21:28:01 +0200 | [diff] [blame] | 45 | |
Kristian Høgsberg | 6d69b6f | 2007-09-17 20:06:45 -0400 | [diff] [blame] | 46 | for (i = j = 0; i < sb->len; i += len, j += newlen) { |
| 47 | eol = memchr(sb->buf + i, '\n', sb->len - i); |
| 48 | len = eol ? eol - (sb->buf + i) + 1 : sb->len - i; |
| 49 | |
| 50 | if (skip_comments && len && sb->buf[i] == '#') { |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 51 | newlen = 0; |
Carlos Rica | 9690c11 | 2007-06-25 21:28:01 +0200 | [diff] [blame] | 52 | continue; |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 53 | } |
Kristian Høgsberg | 6d69b6f | 2007-09-17 20:06:45 -0400 | [diff] [blame] | 54 | newlen = cleanup(sb->buf + i, len); |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 55 | |
| 56 | /* Not just an empty line? */ |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 57 | if (newlen) { |
Kristian Høgsberg | 6d69b6f | 2007-09-17 20:06:45 -0400 | [diff] [blame] | 58 | if (empties > 0 && j > 0) |
| 59 | sb->buf[j++] = '\n'; |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 60 | empties = 0; |
Kristian Høgsberg | 6d69b6f | 2007-09-17 20:06:45 -0400 | [diff] [blame] | 61 | memmove(sb->buf + j, sb->buf + i, newlen); |
| 62 | sb->buf[newlen + j++] = '\n'; |
| 63 | } else { |
| 64 | empties++; |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 65 | } |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 66 | } |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 67 | |
Kristian Høgsberg | 6d69b6f | 2007-09-17 20:06:45 -0400 | [diff] [blame] | 68 | strbuf_setlen(sb, j); |
Lukas Sandström | 7499c99 | 2006-06-13 22:21:53 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 71 | int cmd_stripspace(int argc, const char **argv, const char *prefix) |
Lukas Sandström | 7499c99 | 2006-06-13 22:21:53 +0200 | [diff] [blame] | 72 | { |
Pierre Habouzit | fd17f5b | 2007-09-10 12:35:09 +0200 | [diff] [blame] | 73 | struct strbuf buf; |
Johannes Schindelin | f653aee | 2007-07-23 12:58:27 +0100 | [diff] [blame] | 74 | int strip_comments = 0; |
| 75 | |
| 76 | if (argc > 1 && (!strcmp(argv[1], "-s") || |
| 77 | !strcmp(argv[1], "--strip-comments"))) |
| 78 | strip_comments = 1; |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 79 | |
Pierre Habouzit | fd17f5b | 2007-09-10 12:35:09 +0200 | [diff] [blame] | 80 | strbuf_init(&buf, 0); |
| 81 | if (strbuf_read(&buf, 0, 1024) < 0) |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 82 | die("could not read the input"); |
| 83 | |
Kristian Høgsberg | 6d69b6f | 2007-09-17 20:06:45 -0400 | [diff] [blame] | 84 | stripspace(&buf, strip_comments); |
Carlos Rica | 975e0da | 2007-07-11 20:50:34 +0200 | [diff] [blame] | 85 | |
Pierre Habouzit | fd17f5b | 2007-09-10 12:35:09 +0200 | [diff] [blame] | 86 | write_or_die(1, buf.buf, buf.len); |
| 87 | strbuf_release(&buf); |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 88 | return 0; |
| 89 | } |