changeset 21:e3fab8f1b52c

strip comments.
author David A. Holland
date Mon, 20 Dec 2010 04:15:02 -0500
parents 40748b097655
children cef2dc916269
files macro.c output.c output.h
diffstat 3 files changed, 64 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/macro.c	Mon Dec 20 04:03:35 2010 -0500
+++ b/macro.c	Mon Dec 20 04:15:02 2010 -0500
@@ -620,7 +620,7 @@
 
 static
 void
-expand_send(struct expstate *es, const char *buf, size_t len)
+expand_send(struct expstate *es, struct place *p, const char *buf, size_t len)
 {
 	if (es->tobuf) {
 		if (es->bufpos + len > es->bufmax) {
@@ -635,16 +635,16 @@
 		memcpy(es->buf + es->bufpos, buf, len);
 		es->bufpos += len;
 	} else {
-		output(buf, len);
+		output(p, buf, len);
 	}
 }
 
 static
 void
-expand_send_eof(struct expstate *es)
+expand_send_eof(struct expstate *es, struct place *p)
 {
 	if (es->tobuf) {
-		expand_send(es, "", 1);
+		expand_send(es, p, "", 1);
 		es->bufpos--;
 	} else {
 		output_eof();
@@ -729,11 +729,11 @@
 		if (stringarray_num(&es->args) != 1) {
 			complain(p, "Too many arguments for defined()");
 			complain_fail();
-			expand_send(es, "0", 1);
+			expand_send(es, p, "0", 1);
 			return;
 		}
 		m = macrotable_find(stringarray_get(&es->args, 0), false);
-		expand_send(es, (m != NULL) ? "1" : "0", 1);
+		expand_send(es, p, (m != NULL) ? "1" : "0", 1);
 		return;
 	}
 
@@ -755,8 +755,7 @@
 {
 	switch (es->state) {
 	    case ES_NORMAL:
-		expand_send(es, buf, len);
-		break;
+		expand_send(es, p, buf, len);
 		break;
 	    case ES_WANTLPAREN:
 		break;
@@ -786,7 +785,7 @@
 		}
 		m = macrotable_findlen(buf, len, false);
 		if (m == NULL) {
-			expand_send(es, buf, len);
+			expand_send(es, p, buf, len);
 		} else if (!m->hasparams) {
 			m->inuse = true;
 			assert(expansionitemarray_num(&m->expansion) == 1);
@@ -831,7 +830,7 @@
 {
 	switch (es->state) {
 	    case ES_NORMAL:
-		expand_send(es, buf, len);
+		expand_send(es, p, buf, len);
 		break;
 	    case ES_WANTLPAREN:
 		es->state = ES_NOARG;
@@ -854,7 +853,7 @@
 {
 	switch (es->state) {
 	    case ES_NORMAL:
-		expand_send(es, buf, len);
+		expand_send(es, p, buf, len);
 		break;
 	    case ES_WANTLPAREN:
 		if (es->curmacro) {
@@ -888,7 +887,7 @@
 {
 	switch (es->state) {
 	    case ES_NORMAL:
-		expand_send(es, buf, len);
+		expand_send(es, p, buf, len);
 		break;
 	    case ES_WANTLPAREN:
 		if (es->curmacro) {
@@ -919,7 +918,7 @@
 {
 	switch (es->state) {
 	    case ES_NORMAL:
-		expand_send(es, buf, len);
+		expand_send(es, p, buf, len);
 		break;
 	    case ES_WANTLPAREN:
 		if (es->curmacro) {
@@ -946,7 +945,7 @@
 {
 	switch (es->state) {
 	    case ES_NORMAL:
-		expand_send_eof(es);
+		expand_send_eof(es, p);
 		break;
 	    case ES_WANTLPAREN:
 		if (es->curmacro) {
@@ -1043,6 +1042,7 @@
 macro_sendline(struct place *p, char *buf, size_t len)
 {
 	doexpand(&mainstate, p, buf, len);
+	output(p, "\n", 1);
 }
 
 void
--- a/output.c	Mon Dec 20 04:03:35 2010 -0500
+++ b/output.c	Mon Dec 20 04:15:02 2010 -0500
@@ -4,9 +4,11 @@
 
 #include "utils.h"
 #include "mode.h"
+#include "place.h"
 #include "output.h"
 
 static int outputfd = -1;
+static bool incomment = false;
 
 static
 void
@@ -19,8 +21,9 @@
 	}
 }
 
+static
 void
-output(const char *buf, size_t len)
+dowrite(const char *buf, size_t len)
 {
 	size_t done;
 	ssize_t result;
@@ -51,6 +54,51 @@
 }
 
 void
+output(const struct place *p, const char *buf, size_t len)
+{
+	size_t pos, start;
+	struct place p2;
+
+	start = 0;
+	for (pos = 0; pos < len - 1; pos++) {
+		if (buf[pos] == '/' && buf[pos+1] == '*') {
+			if (incomment && warns.nestcomment) {
+				p2 = *p;
+				p2.column += pos;
+				complain(p, "Warning: %c%c within comment",
+					 '/', '*');
+				if (mode.werror) {
+					complain_failed();
+				}
+			} else if (!incomment) {
+				if (pos > start) {
+					dowrite(buf + start, pos - start);
+				}
+				start = pos;
+				pos += 2;
+				incomment = true;
+				/* cancel out the loop's pos++ */
+				pos--;
+				continue;
+			}
+		} else if (buf[pos] == '*' && buf[pos+1] == '/') {
+			if (incomment) {
+				pos += 2;
+				if (mode.output_retain_comments) {
+					dowrite(buf + start, pos - start);
+				}
+				start = pos;
+				pos += 2;
+				incomment = false;
+				/* cancel out the loop's pos++ */
+				pos--;
+				continue;
+			}
+		}
+	}
+}
+
+void
 output_eof(void)
 {
 	if (outputfd >= 0) {
--- a/output.h	Mon Dec 20 04:03:35 2010 -0500
+++ b/output.h	Mon Dec 20 04:15:02 2010 -0500
@@ -1,2 +1,2 @@
-void output(const char *buf, size_t len);
+void output(const struct place *p, const char *buf, size_t len);
 void output_eof(void);