2006-06-14 04:21:53 +08:00
|
|
|
#include "builtin.h"
|
2005-05-31 03:51:00 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove empty lines from the beginning and end.
|
|
|
|
*
|
|
|
|
* Turn multiple consecutive empty lines into just one
|
2006-04-13 04:10:27 +08:00
|
|
|
* empty line. Return true if it is an incomplete line.
|
2005-05-31 03:51:00 +08:00
|
|
|
*/
|
2006-04-13 04:10:27 +08:00
|
|
|
static int cleanup(char *line)
|
2005-05-31 03:51:00 +08:00
|
|
|
{
|
|
|
|
int len = strlen(line);
|
|
|
|
|
2006-04-15 03:41:51 +08:00
|
|
|
if (len && line[len-1] == '\n') {
|
|
|
|
if (len == 1)
|
|
|
|
return 0;
|
2005-05-31 03:51:00 +08: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-13 04:10:27 +08:00
|
|
|
return 0;
|
2005-05-31 03:51:00 +08:00
|
|
|
}
|
2006-04-13 04:10:27 +08:00
|
|
|
return 1;
|
2005-05-31 03:51:00 +08:00
|
|
|
}
|
|
|
|
|
2006-06-14 04:21:53 +08:00
|
|
|
void stripspace(FILE *in, FILE *out)
|
2005-05-31 03:51:00 +08:00
|
|
|
{
|
|
|
|
int empties = -1;
|
2006-04-13 04:10:27 +08:00
|
|
|
int incomplete = 0;
|
2005-05-31 03:51:00 +08:00
|
|
|
char line[1024];
|
|
|
|
|
2006-06-14 04:21:53 +08:00
|
|
|
while (fgets(line, sizeof(line), in)) {
|
2006-04-13 04:10:27 +08:00
|
|
|
incomplete = cleanup(line);
|
2005-05-31 03:51:00 +08:00
|
|
|
|
|
|
|
/* Not just an empty line? */
|
|
|
|
if (line[0] != '\n') {
|
|
|
|
if (empties > 0)
|
2006-06-14 04:21:53 +08:00
|
|
|
fputc('\n', out);
|
2005-05-31 03:51:00 +08:00
|
|
|
empties = 0;
|
2006-06-14 04:21:53 +08:00
|
|
|
fputs(line, out);
|
2005-05-31 03:51:00 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (empties < 0)
|
|
|
|
continue;
|
|
|
|
empties++;
|
|
|
|
}
|
2006-04-13 04:10:27 +08:00
|
|
|
if (incomplete)
|
2006-06-14 04:21:53 +08:00
|
|
|
fputc('\n', out);
|
|
|
|
}
|
|
|
|
|
2006-07-29 13:44:25 +08:00
|
|
|
int cmd_stripspace(int argc, const char **argv, const char *prefix)
|
2006-06-14 04:21:53 +08:00
|
|
|
{
|
|
|
|
stripspace(stdin, stdout);
|
2005-05-31 03:51:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|