commit 3c2d922256d1c07bd34f50354e8863c14ed36bd3
parent 65f623f36627fbd34784456bd708f04eab393114
Author: nibble <unknown>
Date: Tue, 3 Nov 2009 15:37:46 +0100
* dmc-filter
- Fixed bug related to multiline headers
- More refactoring
Diffstat:
filter.c | | | 27 | +++++++++++++++++---------- |
1 file changed, 17 insertions(+), 10 deletions(-)
diff --git a/filter.c b/filter.c
@@ -4,34 +4,41 @@
#include <stdlib.h>
#include <string.h>
-int main (int argc, char **argv) {
+int main(int argc, char **argv) {
char b[1024], *ptr;
- int edit = argc, print, i;
+ int edit = argc, print = 0, i;
for (i = 0; i < argc; i++)
if (!strcmp (argv[i], "-h")) {
- fprintf (stderr, "Usage: [fields | :] %s [-he] [new fields]\n", argv[0]);
+ fprintf (stderr, "Usage: %s [-h] [headers | :] [-e] [new headers]\n", argv[0]);
return 1;
} else if (!strcmp (argv[i], "-e"))
- edit = i + 1;
+ edit = i;
memset (b, '\0', 1024);
/* Headers */
while (fgets (b, 1023, stdin) && b[0] != '\n')
- for (i = 0, print = 1; i < edit && argv[i]; i++)
+ if (print && (b[0] == ' ' || b[0] == '\t'))
+ fputs (b, stdout);
+ else for (i = 1; i < edit && argv[i]; i++)
if (strstr (b, argv[i])) {
- for (i = edit; i < argc && argv[i]; i++)
+ /* Edit/Remove Headers */
+ print = 1;
+ for (i = edit + 1; i < argc && argv[i]; i++)
if ((ptr = strchr (argv[i], ':')) &&
- !strncmp (b, argv[i], ptr - argv[i])) {
+ !strncmp (b, argv[i], ptr - argv[i])) {
if (ptr[1] != '\0')
puts (argv[i]);
argv[i][0] = '\0';
print = 0;
+ break;
}
if (print) fputs (b, stdout);
- }
+ break;
+ } else if (b[0] != ' ' && b[0] != '\t')
+ print = 0;
/* New Headers */
- for (i = edit; i < argc; i++)
- if ((ptr = strchr (argv[i], ':')) && ptr[1] != '\0') puts (argv[i]);
+ for (i = edit + 1; i < argc; i++)
+ if (argv[i][0]) puts (argv[i]);
if (edit < argc)
puts ("");
/* Body */