diff output.c @ 27:01c3a2088ab4

fix some more bugs
author David A. Holland
date Mon, 20 Dec 2010 05:01:18 -0500
parents daa801fe719e
children 76c114899f63
line wrap: on
line diff
--- a/output.c	Mon Dec 20 05:01:07 2010 -0500
+++ b/output.c	Mon Dec 20 05:01:18 2010 -0500
@@ -1,3 +1,4 @@
+#include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <err.h>
@@ -9,6 +10,9 @@
 
 static int outputfd = -1;
 static bool incomment = false;
+static char *linebuf;
+static size_t linebufpos, linebufmax;
+static struct place linebufplace;
 
 static
 void
@@ -42,8 +46,6 @@
 		output_open();
 	}
 
-	/* XXX this will often come by ones and twos, should buffer */
-
 	done = 0;
 	while (done < len) {
 		result = write(outputfd, buf+done, len-done);
@@ -62,8 +64,10 @@
 	}
 }
 
+
+static
 void
-output(const struct place *p, const char *buf, size_t len)
+filter_output(const struct place *p, const char *buf, size_t len)
 {
 	size_t pos, start;
 	struct place p2;
@@ -97,7 +101,6 @@
 					dowrite(buf + start, pos - start);
 				}
 				start = pos;
-				pos += 2;
 				incomment = false;
 				/* cancel out the loop's pos++ */
 				pos--;
@@ -115,6 +118,30 @@
 }
 
 void
+output(const struct place *p, const char *buf, size_t len)
+{
+	if (linebufpos + len > linebufmax) {
+		if (linebufmax == 0) {
+			linebufmax = 64;
+		}
+		while (linebufpos + len > linebufmax) {
+			linebufmax *= 2;
+		}
+		linebuf = dorealloc(linebuf, linebufmax);
+	}
+	if (linebufpos == 0) {
+		linebufplace = *p;
+	}
+	memcpy(linebuf + linebufpos, buf, len);
+	linebufpos += len;
+
+	if (len == 1 && buf[0] == '\n') {
+		filter_output(&linebufplace, linebuf, linebufpos);
+		linebufpos = 0;
+	}
+}
+
+void
 output_eof(void)
 {
 	if (mode.output_file != NULL && outputfd >= 0) {