RelNotes 1.5.3 updates before -rc4
[git/haiku.git] / builtin-stripspace.c
blob55716873dc4f4a8baa5f2b37d6dca1de488b38e4
1 #include "builtin.h"
2 #include "cache.h"
4 /*
5 * Returns the length of a line, without trailing spaces.
7 * If the line ends with newline, it will be removed too.
8 */
9 static size_t cleanup(char *line, size_t len)
11 if (len) {
12 if (line[len - 1] == '\n')
13 len--;
15 while (len) {
16 unsigned char c = line[len - 1];
17 if (!isspace(c))
18 break;
19 len--;
22 return len;
26 * Remove empty lines from the beginning and end
27 * and also trailing spaces from every line.
29 * Note that the buffer will not be NUL-terminated.
31 * Turn multiple consecutive empty lines between paragraphs
32 * into just one empty line.
34 * If the input has only empty lines and spaces,
35 * no output will be produced.
37 * If last line has a newline at the end, it will be removed.
39 * Enable skip_comments to skip every line starting with "#".
41 size_t stripspace(char *buffer, size_t length, int skip_comments)
43 int empties = -1;
44 size_t i, j, len, newlen;
45 char *eol;
47 for (i = j = 0; i < length; i += len, j += newlen) {
48 eol = memchr(buffer + i, '\n', length - i);
49 len = eol ? eol - (buffer + i) + 1 : length - i;
51 if (skip_comments && len && buffer[i] == '#') {
52 newlen = 0;
53 continue;
55 newlen = cleanup(buffer + i, len);
57 /* Not just an empty line? */
58 if (newlen) {
59 if (empties != -1)
60 buffer[j++] = '\n';
61 if (empties > 0)
62 buffer[j++] = '\n';
63 empties = 0;
64 memmove(buffer + j, buffer + i, newlen);
65 continue;
67 if (empties < 0)
68 continue;
69 empties++;
72 return j;
75 int cmd_stripspace(int argc, const char **argv, const char *prefix)
77 char *buffer;
78 unsigned long size;
80 size = 1024;
81 buffer = xmalloc(size);
82 if (read_fd(0, &buffer, &size)) {
83 free(buffer);
84 die("could not read the input");
87 size = stripspace(buffer, size, 0);
88 write_or_die(1, buffer, size);
89 if (size)
90 putc('\n', stdout);
92 free(buffer);
93 return 0;