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