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