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