comparison 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
comparison
equal deleted inserted replaced
27:01c3a2088ab4 28:8a955e3dda2c
255 unsigned numbuckets, i; 255 unsigned numbuckets, i;
256 256
257 numbuckets = macroarrayarray_num(&macros); 257 numbuckets = macroarrayarray_num(&macros);
258 for (i=0; i<numbuckets; i++) { 258 for (i=0; i<numbuckets; i++) {
259 bucket = macroarrayarray_get(&macros, i); 259 bucket = macroarrayarray_get(&macros, i);
260 macroarray_destroyall(bucket); 260 if (bucket != NULL) {
261 macroarray_destroy(bucket); 261 macroarray_destroyall(bucket);
262 macroarray_destroy(bucket);
263 }
262 } 264 }
263 macroarrayarray_setsize(&macros, 0); 265 macroarrayarray_setsize(&macros, 0);
264 macroarrayarray_cleanup(&macros); 266 macroarrayarray_cleanup(&macros);
265 } 267 }
266 268
681 stringarray_set(&es->args, num - 1, text); 683 stringarray_set(&es->args, num - 1, text);
682 } 684 }
683 685
684 static 686 static
685 char * 687 char *
686 expand_substitute(struct expstate *es) 688 expand_substitute(struct place *p, struct expstate *es)
687 { 689 {
688 struct expansionitem *ei; 690 struct expansionitem *ei;
689 unsigned i, num; 691 unsigned i, num;
690 size_t len; 692 size_t len;
691 char *arg; 693 char *arg;
692 char *ret; 694 char *ret;
695 unsigned numargs, numparams;
696
697 numargs = stringarray_num(&es->args);
698 numparams = stringarray_num(&es->curmacro->params);
699
700 if (numargs == 0 && numparams == 1) {
701 /* no arguments <=> one empty argument */
702 stringarray_add(&es->args, dostrdup(""), NULL);
703 numargs++;
704 }
705 if (numargs != numparams) {
706 complain(p, "Wrong number of arguments for macro %s; "
707 "found %u, expected %u",
708 es->curmacro->name, numargs, numparams);
709 complain_fail();
710 while (numargs < numparams) {
711 stringarray_add(&es->args, dostrdup(""), NULL);
712 numargs++;
713 }
714 }
693 715
694 len = 0; 716 len = 0;
695 num = expansionitemarray_num(&es->curmacro->expansion); 717 num = expansionitemarray_num(&es->curmacro->expansion);
696 for (i=0; i<num; i++) { 718 for (i=0; i<num; i++) {
697 ei = expansionitemarray_get(&es->curmacro->expansion, i); 719 ei = expansionitemarray_get(&es->curmacro->expansion, i);
740 } 762 }
741 763
742 assert(es->curmacro->inuse == false); 764 assert(es->curmacro->inuse == false);
743 es->curmacro->inuse = true; 765 es->curmacro->inuse = true;
744 766
745 newbuf = expand_substitute(es); 767 newbuf = expand_substitute(p, es);
746 newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false); 768 newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false);
747 free(newbuf); 769 free(newbuf);
748 expstate_destroyargs(es); 770 expstate_destroyargs(es);
749 doexpand(es, p, newbuf2, strlen(newbuf2)); 771 doexpand(es, p, newbuf2, strlen(newbuf2));
750 free(newbuf2); 772 free(newbuf2);
982 { 1004 {
983 size_t x; 1005 size_t x;
984 1006
985 while (len > 0) { 1007 while (len > 0) {
986 x = strspn(buf, ws); 1008 x = strspn(buf, ws);
1009 if (x > len) {
1010 /* XXX gross, need strnspn */
1011 x = len;
1012 }
1013
987 if (x > 0) { 1014 if (x > 0) {
988 expand_got_ws(es, p, buf, x); 1015 expand_got_ws(es, p, buf, x);
989 buf += x; 1016 buf += x;
990 len -= x; 1017 len -= x;
1018 continue;
991 } 1019 }
992 1020
993 x = strspn(buf, alnum); 1021 x = strspn(buf, alnum);
1022 if (x > len) {
1023 /* XXX gross, need strnspn */
1024 x = len;
1025 }
1026
994 if (x > 0) { 1027 if (x > 0) {
995 expand_got_word(es, p, buf, x); 1028 expand_got_word(es, p, buf, x);
996 buf += x; 1029 buf += x;
997 len -= x; 1030 len -= x;
998 continue; 1031 continue;