diff macro.c @ 28:8a955e3dda2c posted-20101220

two more tests, more fixes
author David A. Holland
date Mon, 20 Dec 2010 05:42:15 -0500
parents 01c3a2088ab4
children 76c114899f63
line wrap: on
line diff
--- a/macro.c	Mon Dec 20 05:01:18 2010 -0500
+++ b/macro.c	Mon Dec 20 05:42:15 2010 -0500
@@ -257,8 +257,10 @@
 	numbuckets = macroarrayarray_num(&macros);
 	for (i=0; i<numbuckets; i++) {
 		bucket = macroarrayarray_get(&macros, i);
-		macroarray_destroyall(bucket);
-		macroarray_destroy(bucket);
+		if (bucket != NULL) {
+			macroarray_destroyall(bucket);
+			macroarray_destroy(bucket);
+		}
 	}
 	macroarrayarray_setsize(&macros, 0);
 	macroarrayarray_cleanup(&macros);
@@ -683,13 +685,33 @@
 
 static
 char *
-expand_substitute(struct expstate *es)
+expand_substitute(struct place *p, struct expstate *es)
 {
 	struct expansionitem *ei;
 	unsigned i, num;
 	size_t len;
 	char *arg;
 	char *ret;
+	unsigned numargs, numparams;
+
+	numargs = stringarray_num(&es->args);
+	numparams = stringarray_num(&es->curmacro->params);
+
+	if (numargs == 0 && numparams == 1) {
+		/* no arguments <=> one empty argument */
+		stringarray_add(&es->args, dostrdup(""), NULL);
+		numargs++;
+	}
+	if (numargs != numparams) {
+		complain(p, "Wrong number of arguments for macro %s; "
+			 "found %u, expected %u",
+			 es->curmacro->name, numargs, numparams);
+		complain_fail();
+		while (numargs < numparams) {
+			stringarray_add(&es->args, dostrdup(""), NULL);
+			numargs++;
+		}
+	}
 
 	len = 0;
 	num = expansionitemarray_num(&es->curmacro->expansion);
@@ -742,7 +764,7 @@
 	assert(es->curmacro->inuse == false);
 	es->curmacro->inuse = true;
 
-	newbuf = expand_substitute(es);
+	newbuf = expand_substitute(p, es);
 	newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false);
 	free(newbuf);
 	expstate_destroyargs(es);
@@ -984,13 +1006,24 @@
 
 	while (len > 0) {
 		x = strspn(buf, ws);
+		if (x > len) {
+			/* XXX gross, need strnspn */
+			x = len;
+		}
+
 		if (x > 0) {
 			expand_got_ws(es, p, buf, x);
 			buf += x;
 			len -= x;
+			continue;
 		}
 
 		x = strspn(buf, alnum);
+		if (x > len) {
+			/* XXX gross, need strnspn */
+			x = len;
+		}
+
 		if (x > 0) {
 			expand_got_word(es, p, buf, x);
 			buf += x;