You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.0 KiB
62 lines
1.0 KiB
20 years ago
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <ctype.h>
|
||
19 years ago
|
#include "builtin.h"
|
||
20 years ago
|
|
||
|
/*
|
||
|
* Remove empty lines from the beginning and end.
|
||
|
*
|
||
|
* Turn multiple consecutive empty lines into just one
|
||
19 years ago
|
* empty line. Return true if it is an incomplete line.
|
||
20 years ago
|
*/
|
||
19 years ago
|
static int cleanup(char *line)
|
||
20 years ago
|
{
|
||
|
int len = strlen(line);
|
||
|
|
||
19 years ago
|
if (len && line[len-1] == '\n') {
|
||
|
if (len == 1)
|
||
|
return 0;
|
||
20 years ago
|
do {
|
||
|
unsigned char c = line[len-2];
|
||
|
if (!isspace(c))
|
||
|
break;
|
||
|
line[len-2] = '\n';
|
||
|
len--;
|
||
|
line[len] = 0;
|
||
|
} while (len > 1);
|
||
19 years ago
|
return 0;
|
||
20 years ago
|
}
|
||
19 years ago
|
return 1;
|
||
20 years ago
|
}
|
||
|
|
||
19 years ago
|
void stripspace(FILE *in, FILE *out)
|
||
20 years ago
|
{
|
||
|
int empties = -1;
|
||
19 years ago
|
int incomplete = 0;
|
||
20 years ago
|
char line[1024];
|
||
|
|
||
19 years ago
|
while (fgets(line, sizeof(line), in)) {
|
||
19 years ago
|
incomplete = cleanup(line);
|
||
20 years ago
|
|
||
|
/* Not just an empty line? */
|
||
|
if (line[0] != '\n') {
|
||
|
if (empties > 0)
|
||
19 years ago
|
fputc('\n', out);
|
||
20 years ago
|
empties = 0;
|
||
19 years ago
|
fputs(line, out);
|
||
20 years ago
|
continue;
|
||
|
}
|
||
|
if (empties < 0)
|
||
|
continue;
|
||
|
empties++;
|
||
|
}
|
||
19 years ago
|
if (incomplete)
|
||
19 years ago
|
fputc('\n', out);
|
||
|
}
|
||
|
|
||
|
int cmd_stripspace(int argc, const char **argv, char **envp)
|
||
|
{
|
||
|
stripspace(stdin, stdout);
|
||
20 years ago
|
return 0;
|
||
|
}
|