]>
Commit | Line | Data |
---|---|---|
7499c996 | 1 | #include "builtin.h" |
a3e870f2 LT |
2 | |
3 | /* | |
4 | * Remove empty lines from the beginning and end. | |
5 | * | |
6 | * Turn multiple consecutive empty lines into just one | |
f4ee3eb6 | 7 | * empty line. Return true if it is an incomplete line. |
a3e870f2 | 8 | */ |
f4ee3eb6 | 9 | static int cleanup(char *line) |
a3e870f2 LT |
10 | { |
11 | int len = strlen(line); | |
12 | ||
5cf7e21f JH |
13 | if (len && line[len-1] == '\n') { |
14 | if (len == 1) | |
15 | return 0; | |
a3e870f2 LT |
16 | do { |
17 | unsigned char c = line[len-2]; | |
18 | if (!isspace(c)) | |
19 | break; | |
20 | line[len-2] = '\n'; | |
21 | len--; | |
22 | line[len] = 0; | |
23 | } while (len > 1); | |
f4ee3eb6 | 24 | return 0; |
a3e870f2 | 25 | } |
f4ee3eb6 | 26 | return 1; |
a3e870f2 LT |
27 | } |
28 | ||
7499c996 | 29 | void stripspace(FILE *in, FILE *out) |
a3e870f2 LT |
30 | { |
31 | int empties = -1; | |
f4ee3eb6 | 32 | int incomplete = 0; |
a3e870f2 LT |
33 | char line[1024]; |
34 | ||
7499c996 | 35 | while (fgets(line, sizeof(line), in)) { |
f4ee3eb6 | 36 | incomplete = cleanup(line); |
a3e870f2 LT |
37 | |
38 | /* Not just an empty line? */ | |
39 | if (line[0] != '\n') { | |
40 | if (empties > 0) | |
7499c996 | 41 | fputc('\n', out); |
a3e870f2 | 42 | empties = 0; |
7499c996 | 43 | fputs(line, out); |
a3e870f2 LT |
44 | continue; |
45 | } | |
46 | if (empties < 0) | |
47 | continue; | |
48 | empties++; | |
49 | } | |
f4ee3eb6 | 50 | if (incomplete) |
7499c996 LS |
51 | fputc('\n', out); |
52 | } | |
53 | ||
a633fca0 | 54 | int cmd_stripspace(int argc, const char **argv, const char *prefix) |
7499c996 LS |
55 | { |
56 | stripspace(stdin, stdout); | |
a3e870f2 LT |
57 | return 0; |
58 | } |