2006-06-13 22:21:53 +02:00
|
|
|
#include "builtin.h"
|
2007-06-25 21:28:01 +02:00
|
|
|
#include "cache.h"
|
2005-05-30 21:51:00 +02:00
|
|
|
|
|
|
|
/*
|
2007-06-25 21:28:01 +02:00
|
|
|
* Remove trailing spaces from a line.
|
2005-05-30 21:51:00 +02:00
|
|
|
*
|
2007-06-25 21:28:01 +02:00
|
|
|
* If the line ends with newline, it will be removed too.
|
|
|
|
* Returns the new length of the string.
|
2005-05-30 21:51:00 +02:00
|
|
|
*/
|
2007-06-25 21:28:01 +02:00
|
|
|
static int cleanup(char *line, int len)
|
2005-05-30 21:51:00 +02:00
|
|
|
{
|
2007-06-25 21:28:01 +02:00
|
|
|
if (len) {
|
|
|
|
if (line[len - 1] == '\n')
|
|
|
|
len--;
|
2005-05-30 21:51:00 +02:00
|
|
|
|
2007-06-25 21:28:01 +02:00
|
|
|
while (len) {
|
|
|
|
unsigned char c = line[len - 1];
|
2005-05-30 21:51:00 +02:00
|
|
|
if (!isspace(c))
|
|
|
|
break;
|
|
|
|
len--;
|
2007-06-25 21:28:01 +02:00
|
|
|
}
|
|
|
|
line[len] = 0;
|
2005-05-30 21:51:00 +02:00
|
|
|
}
|
2007-06-25 21:28:01 +02:00
|
|
|
return len;
|
2005-05-30 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2007-06-25 21:28:01 +02:00
|
|
|
/*
|
|
|
|
* Remove empty lines from the beginning and end
|
|
|
|
* and also trailing spaces from every line.
|
|
|
|
*
|
|
|
|
* Turn multiple consecutive empty lines between paragraphs
|
|
|
|
* into just one empty line.
|
|
|
|
*
|
|
|
|
* If the input has only empty lines and spaces,
|
|
|
|
* no output will be produced.
|
|
|
|
*
|
|
|
|
* Enable skip_comments to skip every line starting with "#".
|
|
|
|
*/
|
|
|
|
void stripspace(FILE *in, FILE *out, int skip_comments)
|
2005-05-30 21:51:00 +02:00
|
|
|
{
|
|
|
|
int empties = -1;
|
2007-06-25 21:28:01 +02:00
|
|
|
int alloc = 1024;
|
|
|
|
char *line = xmalloc(alloc);
|
|
|
|
|
|
|
|
while (fgets(line, alloc, in)) {
|
|
|
|
int len = strlen(line);
|
2005-05-30 21:51:00 +02:00
|
|
|
|
2007-06-25 21:28:01 +02:00
|
|
|
while (len == alloc - 1 && line[len - 1] != '\n') {
|
|
|
|
alloc = alloc_nr(alloc);
|
|
|
|
line = xrealloc(line, alloc);
|
|
|
|
fgets(line + len, alloc - len, in);
|
|
|
|
len += strlen(line + len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skip_comments && line[0] == '#')
|
|
|
|
continue;
|
|
|
|
len = cleanup(line, len);
|
2005-05-30 21:51:00 +02:00
|
|
|
|
|
|
|
/* Not just an empty line? */
|
2007-06-25 21:28:01 +02:00
|
|
|
if (len) {
|
2005-05-30 21:51:00 +02:00
|
|
|
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);
|
2007-06-25 21:28:01 +02:00
|
|
|
fputc('\n', out);
|
2005-05-30 21:51:00 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (empties < 0)
|
|
|
|
continue;
|
|
|
|
empties++;
|
|
|
|
}
|
2007-06-25 21:28:01 +02:00
|
|
|
free(line);
|
2006-06-13 22:21:53 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2007-06-25 21:28:01 +02:00
|
|
|
stripspace(stdin, stdout, 0);
|
2005-05-30 21:51:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|