2005-05-30 21:51:00 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove empty lines from the beginning and end.
|
|
|
|
*
|
|
|
|
* Turn multiple consecutive empty lines into just one
|
2006-04-12 22:10:27 +02:00
|
|
|
* empty line. Return true if it is an incomplete line.
|
2005-05-30 21:51:00 +02:00
|
|
|
*/
|
2006-04-12 22:10:27 +02:00
|
|
|
static int cleanup(char *line)
|
2005-05-30 21:51:00 +02:00
|
|
|
{
|
|
|
|
int len = strlen(line);
|
|
|
|
|
2006-04-14 21:41:51 +02:00
|
|
|
if (len && line[len-1] == '\n') {
|
|
|
|
if (len == 1)
|
|
|
|
return 0;
|
2005-05-30 21:51:00 +02:00
|
|
|
do {
|
|
|
|
unsigned char c = line[len-2];
|
|
|
|
if (!isspace(c))
|
|
|
|
break;
|
|
|
|
line[len-2] = '\n';
|
|
|
|
len--;
|
|
|
|
line[len] = 0;
|
|
|
|
} while (len > 1);
|
2006-04-12 22:10:27 +02:00
|
|
|
return 0;
|
2005-05-30 21:51:00 +02:00
|
|
|
}
|
2006-04-12 22:10:27 +02:00
|
|
|
return 1;
|
2005-05-30 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int empties = -1;
|
2006-04-12 22:10:27 +02:00
|
|
|
int incomplete = 0;
|
2005-05-30 21:51:00 +02:00
|
|
|
char line[1024];
|
|
|
|
|
|
|
|
while (fgets(line, sizeof(line), stdin)) {
|
2006-04-12 22:10:27 +02:00
|
|
|
incomplete = cleanup(line);
|
2005-05-30 21:51:00 +02:00
|
|
|
|
|
|
|
/* Not just an empty line? */
|
|
|
|
if (line[0] != '\n') {
|
|
|
|
if (empties > 0)
|
|
|
|
putchar('\n');
|
|
|
|
empties = 0;
|
|
|
|
fputs(line, stdout);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (empties < 0)
|
|
|
|
continue;
|
|
|
|
empties++;
|
|
|
|
}
|
2006-04-12 22:10:27 +02:00
|
|
|
if (incomplete)
|
|
|
|
putchar('\n');
|
2005-05-30 21:51:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|