Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <ctype.h> |
Lukas Sandström | 7499c99 | 2006-06-13 22:21:53 +0200 | [diff] [blame] | 4 | #include "builtin.h" |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 5 | |
| 6 | /* |
| 7 | * Remove empty lines from the beginning and end. |
| 8 | * |
| 9 | * Turn multiple consecutive empty lines into just one |
Junio C Hamano | f4ee3eb | 2006-04-12 13:10:27 -0700 | [diff] [blame] | 10 | * empty line. Return true if it is an incomplete line. |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 11 | */ |
Junio C Hamano | f4ee3eb | 2006-04-12 13:10:27 -0700 | [diff] [blame] | 12 | static int cleanup(char *line) |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 13 | { |
| 14 | int len = strlen(line); |
| 15 | |
Junio C Hamano | 5cf7e21 | 2006-04-14 12:41:51 -0700 | [diff] [blame] | 16 | if (len && line[len-1] == '\n') { |
| 17 | if (len == 1) |
| 18 | return 0; |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 19 | do { |
| 20 | unsigned char c = line[len-2]; |
| 21 | if (!isspace(c)) |
| 22 | break; |
| 23 | line[len-2] = '\n'; |
| 24 | len--; |
| 25 | line[len] = 0; |
| 26 | } while (len > 1); |
Junio C Hamano | f4ee3eb | 2006-04-12 13:10:27 -0700 | [diff] [blame] | 27 | return 0; |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 28 | } |
Junio C Hamano | f4ee3eb | 2006-04-12 13:10:27 -0700 | [diff] [blame] | 29 | return 1; |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Lukas Sandström | 7499c99 | 2006-06-13 22:21:53 +0200 | [diff] [blame] | 32 | void stripspace(FILE *in, FILE *out) |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 33 | { |
| 34 | int empties = -1; |
Junio C Hamano | f4ee3eb | 2006-04-12 13:10:27 -0700 | [diff] [blame] | 35 | int incomplete = 0; |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 36 | char line[1024]; |
| 37 | |
Lukas Sandström | 7499c99 | 2006-06-13 22:21:53 +0200 | [diff] [blame] | 38 | while (fgets(line, sizeof(line), in)) { |
Junio C Hamano | f4ee3eb | 2006-04-12 13:10:27 -0700 | [diff] [blame] | 39 | incomplete = cleanup(line); |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 40 | |
| 41 | /* Not just an empty line? */ |
| 42 | if (line[0] != '\n') { |
| 43 | if (empties > 0) |
Lukas Sandström | 7499c99 | 2006-06-13 22:21:53 +0200 | [diff] [blame] | 44 | fputc('\n', out); |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 45 | empties = 0; |
Lukas Sandström | 7499c99 | 2006-06-13 22:21:53 +0200 | [diff] [blame] | 46 | fputs(line, out); |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 47 | continue; |
| 48 | } |
| 49 | if (empties < 0) |
| 50 | continue; |
| 51 | empties++; |
| 52 | } |
Junio C Hamano | f4ee3eb | 2006-04-12 13:10:27 -0700 | [diff] [blame] | 53 | if (incomplete) |
Lukas Sandström | 7499c99 | 2006-06-13 22:21:53 +0200 | [diff] [blame] | 54 | fputc('\n', out); |
| 55 | } |
| 56 | |
| 57 | int cmd_stripspace(int argc, const char **argv, char **envp) |
| 58 | { |
| 59 | stripspace(stdin, stdout); |
Linus Torvalds | a3e870f | 2005-05-30 12:51:00 -0700 | [diff] [blame] | 60 | return 0; |
| 61 | } |