2006-06-13 22:21:53 +02:00
|
|
|
#include "builtin.h"
|
2005-05-30 21:51:00 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
2006-06-13 22:21:53 +02:00
|
|
|
void stripspace(FILE *in, FILE *out)
|
2005-05-30 21:51:00 +02:00
|
|
|
{
|
|
|
|
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];
|
|
|
|
|
2006-06-13 22:21:53 +02:00
|
|
|
while (fgets(line, sizeof(line), in)) {
|
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)
|
2006-06-13 22:21:53 +02:00
|
|
|
fputc('\n', out);
|
2005-05-30 21:51:00 +02:00
|
|
|
empties = 0;
|
2006-06-13 22:21:53 +02:00
|
|
|
fputs(line, out);
|
2005-05-30 21:51:00 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (empties < 0)
|
|
|
|
continue;
|
|
|
|
empties++;
|
|
|
|
}
|
2006-04-12 22:10:27 +02:00
|
|
|
if (incomplete)
|
2006-06-13 22:21:53 +02:00
|
|
|
fputc('\n', out);
|
|
|
|
}
|
|
|
|
|
2006-07-29 07:44:25 +02:00
|
|
|
int cmd_stripspace(int argc, const char **argv, const char *prefix)
|
2006-06-13 22:21:53 +02:00
|
|
|
{
|
|
|
|
stripspace(stdin, stdout);
|
2005-05-30 21:51:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|