Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate macro.c @ 83:3e505c16b0b0
Accept # by itself, including with a comment after it.
author | David A. Holland |
---|---|
date | Mon, 10 Jun 2013 21:33:31 -0400 |
parents | bbbf71859a21 |
children | 7e4723d34248 |
rev | line source |
---|---|
30 | 1 /*- |
2 * Copyright (c) 2010 The NetBSD Foundation, Inc. | |
3 * All rights reserved. | |
4 * | |
5 * This code is derived from software contributed to The NetBSD Foundation | |
6 * by David A. Holland. | |
7 * | |
8 * Redistribution and use in source and binary forms, with or without | |
9 * modification, are permitted provided that the following conditions | |
10 * are met: | |
11 * 1. Redistributions of source code must retain the above copyright | |
12 * notice, this list of conditions and the following disclaimer. | |
13 * 2. Redistributions in binary form must reproduce the above copyright | |
14 * notice, this list of conditions and the following disclaimer in the | |
15 * documentation and/or other materials provided with the distribution. | |
16 * | |
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | |
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
27 * POSSIBILITY OF SUCH DAMAGE. | |
28 */ | |
29 | |
33 | 30 #include <stdint.h> |
17 | 31 #include <stdlib.h> |
32 #include <string.h> | |
33 | |
34 #include "array.h" | |
35 #include "mode.h" | |
36 #include "place.h" | |
37 #include "macro.h" | |
19 | 38 #include "output.h" |
39 | |
40 struct expansionitem { | |
41 bool isstring; | |
42 union { | |
43 char *string; | |
44 unsigned param; | |
45 }; | |
46 }; | |
47
2e25e55dba6b
Fix inline usage as per the version in dholland-make2.
David A. Holland
parents:
42
diff
changeset
|
47 DECLARRAY(expansionitem, static __unused); |
2e25e55dba6b
Fix inline usage as per the version in dholland-make2.
David A. Holland
parents:
42
diff
changeset
|
48 DEFARRAY(expansionitem, static); |
17 | 49 |
50 struct macro { | |
51 struct place defplace; | |
52 struct place expansionplace; | |
53 unsigned hash; | |
54 char *name; | |
18 | 55 bool hasparams; |
56 struct stringarray params; | |
19 | 57 struct expansionitemarray expansion; |
58 bool inuse; | |
17 | 59 }; |
47
2e25e55dba6b
Fix inline usage as per the version in dholland-make2.
David A. Holland
parents:
42
diff
changeset
|
60 DECLARRAY(macro, static __unused); |
2e25e55dba6b
Fix inline usage as per the version in dholland-make2.
David A. Holland
parents:
42
diff
changeset
|
61 DEFARRAY(macro, static); |
2e25e55dba6b
Fix inline usage as per the version in dholland-make2.
David A. Holland
parents:
42
diff
changeset
|
62 DECLARRAY(macroarray, static __unused); |
2e25e55dba6b
Fix inline usage as per the version in dholland-make2.
David A. Holland
parents:
42
diff
changeset
|
63 DEFARRAY(macroarray, static); |
17 | 64 |
65 static struct macroarrayarray macros; | |
66 static unsigned total_macros; | |
67 static unsigned hashmask; | |
68 | |
69 //////////////////////////////////////////////////////////// | |
70 // macro structure ops | |
71 | |
72 static | |
19 | 73 struct expansionitem * |
74 expansionitem_create_string(const char *string) | |
75 { | |
76 struct expansionitem *ei; | |
77 | |
78 ei = domalloc(sizeof(*ei)); | |
79 ei->isstring = true; | |
80 ei->string = dostrdup(string); | |
81 return ei; | |
82 } | |
83 | |
84 static | |
85 struct expansionitem * | |
86 expansionitem_create_stringlen(const char *string, size_t len) | |
87 { | |
88 struct expansionitem *ei; | |
89 | |
90 ei = domalloc(sizeof(*ei)); | |
91 ei->isstring = true; | |
92 ei->string = dostrndup(string, len); | |
93 return ei; | |
94 } | |
95 | |
96 static | |
97 struct expansionitem * | |
98 expansionitem_create_param(unsigned param) | |
99 { | |
100 struct expansionitem *ei; | |
101 | |
102 ei = domalloc(sizeof(*ei)); | |
103 ei->isstring = false; | |
104 ei->param = param; | |
105 return ei; | |
106 } | |
107 | |
108 static | |
109 void | |
110 expansionitem_destroy(struct expansionitem *ei) | |
111 { | |
112 if (ei->isstring) { | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
113 dostrfree(ei->string); |
19 | 114 } |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
115 dofree(ei, sizeof(*ei)); |
19 | 116 } |
117 | |
118 static | |
119 bool | |
120 expansionitem_eq(const struct expansionitem *ei1, | |
121 const struct expansionitem *ei2) | |
122 { | |
123 if (ei1->isstring != ei2->isstring) { | |
124 return false; | |
125 } | |
126 if (ei1->isstring) { | |
127 if (strcmp(ei1->string, ei2->string) != 0) { | |
128 return false; | |
129 } | |
130 } else { | |
131 if (ei1->param != ei2->param) { | |
132 return false; | |
133 } | |
134 } | |
135 return true; | |
136 } | |
137 | |
138 static | |
17 | 139 struct macro * |
18 | 140 macro_create(struct place *p1, const char *name, unsigned hash, |
19 | 141 struct place *p2) |
17 | 142 { |
143 struct macro *m; | |
144 | |
145 m = domalloc(sizeof(*m)); | |
146 m->defplace = *p1; | |
147 m->expansionplace = *p2; | |
148 m->hash = hash; | |
149 m->name = dostrdup(name); | |
18 | 150 m->hasparams = false; |
151 stringarray_init(&m->params); | |
19 | 152 expansionitemarray_init(&m->expansion); |
153 m->inuse = false; | |
17 | 154 return m; |
155 } | |
156 | |
19 | 157 DESTROYALL_ARRAY(expansionitem, ); |
158 | |
17 | 159 static |
160 void | |
161 macro_destroy(struct macro *m) | |
162 { | |
19 | 163 expansionitemarray_destroyall(&m->expansion); |
164 expansionitemarray_cleanup(&m->expansion); | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
165 dostrfree(m->name); |
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
166 dofree(m, sizeof(*m)); |
17 | 167 } |
168 | |
18 | 169 static |
170 bool | |
171 macro_eq(const struct macro *m1, const struct macro *m2) | |
172 { | |
173 unsigned num1, num2, i; | |
19 | 174 struct expansionitem *ei1, *ei2; |
18 | 175 const char *p1, *p2; |
176 | |
177 if (strcmp(m1->name, m2->name) != 0) { | |
178 return false; | |
179 } | |
180 | |
181 if (m1->hasparams != m2->hasparams) { | |
182 return false; | |
183 } | |
184 | |
19 | 185 num1 = expansionitemarray_num(&m1->expansion); |
186 num2 = expansionitemarray_num(&m2->expansion); | |
187 if (num1 != num2) { | |
18 | 188 return false; |
189 } | |
190 | |
19 | 191 for (i=0; i<num1; i++) { |
192 ei1 = expansionitemarray_get(&m1->expansion, i); | |
193 ei2 = expansionitemarray_get(&m2->expansion, i); | |
194 if (!expansionitem_eq(ei1, ei2)) { | |
195 return false; | |
196 } | |
197 } | |
198 | |
18 | 199 num1 = stringarray_num(&m1->params); |
200 num2 = stringarray_num(&m2->params); | |
201 if (num1 != num2) { | |
202 return false; | |
203 } | |
204 | |
205 for (i=0; i<num1; i++) { | |
206 p1 = stringarray_get(&m1->params, i); | |
207 p2 = stringarray_get(&m2->params, i); | |
208 if (strcmp(p1, p2) != 0) { | |
209 return false; | |
210 } | |
211 } | |
212 return true; | |
213 } | |
214 | |
17 | 215 //////////////////////////////////////////////////////////// |
216 // macro table | |
217 | |
218 /* | |
219 * Unless I've screwed up, this is something called Fletcher's Checksum | |
220 * that showed up in Dr. Dobbs in, according to my notes, May 1992. The | |
221 * implementation is new. | |
222 */ | |
223 static | |
224 unsigned | |
19 | 225 hashfunc(const char *s, size_t len) |
17 | 226 { |
227 uint16_t x1, x2, a; | |
19 | 228 size_t i; |
17 | 229 |
230 x1 = (uint16_t) (len >> 16); | |
231 x2 = (uint16_t) (len); | |
232 if (x1==0) { | |
233 x1++; | |
234 } | |
235 if (x2==0) { | |
236 x2++; | |
237 } | |
238 | |
239 for (i=0; i<len; i+=2) { | |
240 if (i==len-1) { | |
241 a = (unsigned char)s[i]; | |
242 /* don't run off the end of the array */ | |
243 } | |
244 else { | |
245 a = (unsigned char)s[i] + | |
246 ((uint16_t)(unsigned char)s[i+1] << 8); | |
247 } | |
248 x1 += a; | |
249 if (x1 < a) { | |
250 x1++; | |
251 } | |
252 x2 += x1; | |
253 if (x2 < x1) { | |
254 x2++; | |
255 } | |
256 } | |
257 | |
258 x1 ^= 0xffff; | |
259 x2 ^= 0xffff; | |
260 return ((uint32_t)x2)*65535U + x1; | |
261 } | |
262 | |
263 static | |
264 void | |
265 macrotable_init(void) | |
266 { | |
267 unsigned i; | |
268 | |
269 macroarrayarray_init(¯os); | |
270 macroarrayarray_setsize(¯os, 4); | |
271 for (i=0; i<4; i++) { | |
272 macroarrayarray_set(¯os, i, NULL); | |
273 } | |
274 total_macros = 0; | |
275 hashmask = 0x3; | |
276 } | |
277 | |
278 DESTROYALL_ARRAY(macro, ); | |
279 | |
280 static | |
281 void | |
282 macrotable_cleanup(void) | |
283 { | |
284 struct macroarray *bucket; | |
285 unsigned numbuckets, i; | |
286 | |
287 numbuckets = macroarrayarray_num(¯os); | |
288 for (i=0; i<numbuckets; i++) { | |
289 bucket = macroarrayarray_get(¯os, i); | |
28 | 290 if (bucket != NULL) { |
291 macroarray_destroyall(bucket); | |
292 macroarray_destroy(bucket); | |
293 } | |
17 | 294 } |
295 macroarrayarray_setsize(¯os, 0); | |
296 macroarrayarray_cleanup(¯os); | |
297 } | |
298 | |
299 static | |
300 struct macro * | |
19 | 301 macrotable_findlen(const char *name, size_t len, bool remove) |
17 | 302 { |
303 unsigned hash; | |
304 struct macroarray *bucket; | |
305 struct macro *m, *m2; | |
306 unsigned i, num; | |
19 | 307 size_t mlen; |
17 | 308 |
19 | 309 hash = hashfunc(name, len); |
17 | 310 bucket = macroarrayarray_get(¯os, hash & hashmask); |
311 if (bucket == NULL) { | |
312 return NULL; | |
313 } | |
314 num = macroarray_num(bucket); | |
315 for (i=0; i<num; i++) { | |
316 m = macroarray_get(bucket, i); | |
317 if (hash != m->hash) { | |
318 continue; | |
319 } | |
19 | 320 mlen = strlen(m->name); |
321 if (len == mlen && !memcmp(name, m->name, len)) { | |
17 | 322 if (remove) { |
323 if (i < num-1) { | |
324 m2 = macroarray_get(bucket, num-1); | |
325 macroarray_set(bucket, i, m2); | |
326 } | |
327 macroarray_setsize(bucket, num-1); | |
328 total_macros--; | |
329 } | |
330 return m; | |
331 } | |
332 } | |
333 return NULL; | |
334 } | |
335 | |
336 static | |
19 | 337 struct macro * |
338 macrotable_find(const char *name, bool remove) | |
339 { | |
340 return macrotable_findlen(name, strlen(name), remove); | |
341 } | |
342 | |
343 static | |
17 | 344 void |
345 macrotable_rehash(void) | |
346 { | |
347 struct macroarray *newbucket, *oldbucket; | |
348 struct macro *m; | |
349 unsigned newmask, tossbit; | |
350 unsigned numbuckets, i; | |
351 unsigned oldnum, j, k; | |
352 | |
353 numbuckets = macroarrayarray_num(¯os); | |
354 macroarrayarray_setsize(¯os, numbuckets*2); | |
355 | |
356 assert(hashmask == numbuckets - 1); | |
357 newmask = (hashmask << 1) | 1U; | |
74 | 358 tossbit = newmask & ~hashmask; |
17 | 359 hashmask = newmask; |
360 | |
361 for (i=0; i<numbuckets; i++) { | |
362 newbucket = NULL; | |
363 oldbucket = macroarrayarray_get(¯os, i); | |
74 | 364 if (oldbucket == NULL) { |
365 macroarrayarray_set(¯os, numbuckets + i, NULL); | |
366 continue; | |
367 } | |
17 | 368 oldnum = macroarray_num(oldbucket); |
369 for (j=0; j<oldnum; j++) { | |
370 m = macroarray_get(oldbucket, j); | |
371 if (m->hash & tossbit) { | |
372 if (newbucket == NULL) { | |
373 newbucket = macroarray_create(); | |
374 } | |
375 macroarray_set(oldbucket, j, NULL); | |
376 macroarray_add(newbucket, m, NULL); | |
377 } | |
378 } | |
379 for (j=k=0; j<oldnum; j++) { | |
380 m = macroarray_get(oldbucket, j); | |
74 | 381 if (m != NULL) { |
382 if (k < j) { | |
383 macroarray_set(oldbucket, k, m); | |
384 } | |
385 k++; | |
17 | 386 } |
387 } | |
388 macroarray_setsize(oldbucket, k); | |
389 macroarrayarray_set(¯os, numbuckets + i, newbucket); | |
390 } | |
391 } | |
392 | |
393 static | |
394 void | |
395 macrotable_add(struct macro *m) | |
396 { | |
397 unsigned hash; | |
398 struct macroarray *bucket; | |
399 unsigned numbuckets; | |
400 | |
401 numbuckets = macroarrayarray_num(¯os); | |
402 if (total_macros > 0 && total_macros / numbuckets > 9) { | |
403 macrotable_rehash(); | |
404 } | |
405 | |
19 | 406 hash = hashfunc(m->name, strlen(m->name)); |
17 | 407 bucket = macroarrayarray_get(¯os, hash & hashmask); |
408 if (bucket == NULL) { | |
409 bucket = macroarray_create(); | |
410 macroarrayarray_set(¯os, hash & hashmask, bucket); | |
411 } | |
412 macroarray_add(bucket, m, NULL); | |
413 total_macros++; | |
414 } | |
415 | |
416 //////////////////////////////////////////////////////////// | |
417 // external macro definition interface | |
418 | |
18 | 419 static |
420 struct macro * | |
421 macro_define_common_start(struct place *p1, const char *macro, | |
19 | 422 struct place *p2) |
17 | 423 { |
424 struct macro *m; | |
19 | 425 unsigned hash; |
17 | 426 |
18 | 427 if (!is_identifier(macro)) { |
428 complain(p1, "Invalid macro name %s", macro); | |
429 complain_fail(); | |
430 } | |
431 | |
19 | 432 hash = hashfunc(macro, strlen(macro)); |
433 m = macro_create(p1, macro, hash, p2); | |
18 | 434 return m; |
435 } | |
436 | |
437 static | |
438 void | |
439 macro_define_common_end(struct macro *m) | |
440 { | |
441 struct macro *oldm; | |
442 bool ok; | |
443 | |
444 oldm = macrotable_find(m->name, false); | |
445 if (oldm != NULL) { | |
446 ok = macro_eq(m, oldm); | |
447 if (ok) { | |
448 complain(&m->defplace, | |
449 "Warning: redefinition of %s", m->name); | |
17 | 450 if (mode.werror) { |
451 complain_fail(); | |
452 } | |
18 | 453 } else { |
454 complain(&m->defplace, | |
455 "Redefinition of %s is not identical", | |
456 m->name); | |
457 complain_fail(); | |
17 | 458 } |
18 | 459 complain(&oldm->defplace, "Previous definition was here"); |
460 macro_destroy(m); | |
17 | 461 return; |
462 } | |
18 | 463 macrotable_add(m); |
464 } | |
17 | 465 |
18 | 466 static |
467 void | |
468 macro_parse_parameters(struct macro *m, struct place *p, const char *params) | |
469 { | |
470 size_t len; | |
471 const char *s; | |
472 char *param; | |
473 | |
474 while (params != NULL) { | |
475 len = strspn(params, ws); | |
476 params += len; | |
477 p->column += len; | |
478 s = strchr(params, ','); | |
479 if (s) { | |
480 len = s-params; | |
481 param = dostrndup(params, len); | |
482 s++; | |
483 } else { | |
484 len = strlen(params); | |
485 param = dostrndup(params, len); | |
486 } | |
487 notrailingws(param, strlen(param)); | |
488 if (!is_identifier(param)) { | |
489 complain(p, "Invalid macro parameter name %s", param); | |
490 complain_fail(); | |
491 } else { | |
492 stringarray_add(&m->params, param, NULL); | |
493 } | |
494 params = s; | |
495 p->column += len; | |
496 } | |
497 } | |
498 | |
19 | 499 static |
500 bool | |
501 isparam(struct macro *m, const char *name, size_t len, unsigned *num_ret) | |
502 { | |
503 unsigned num, i; | |
504 const char *param; | |
505 | |
506 num = stringarray_num(&m->params); | |
507 for (i=0; i<num; i++) { | |
508 param = stringarray_get(&m->params, i); | |
27 | 509 if (strlen(param) == len && !memcmp(name, param, len)) { |
19 | 510 *num_ret = i; |
511 return true; | |
512 } | |
513 } | |
514 return false; | |
515 } | |
516 | |
517 static | |
518 void | |
519 macro_parse_expansion(struct macro *m, const char *buf) | |
520 { | |
521 size_t blockstart, wordstart, pos; | |
522 struct expansionitem *ei; | |
523 unsigned param; | |
524 | |
525 pos = blockstart = 0; | |
526 while (buf[pos] != '\0') { | |
527 pos += strspn(buf+pos, ws); | |
528 if (strchr(alnum, buf[pos])) { | |
529 wordstart = pos; | |
530 pos += strspn(buf+pos, alnum); | |
531 if (isparam(m, buf+wordstart, pos-wordstart, ¶m)) { | |
532 if (pos > blockstart) { | |
533 ei = expansionitem_create_stringlen( | |
534 buf + blockstart, | |
27 | 535 wordstart - blockstart); |
19 | 536 expansionitemarray_add(&m->expansion, |
537 ei, NULL); | |
538 } | |
539 ei = expansionitem_create_param(param); | |
540 expansionitemarray_add(&m->expansion, ei,NULL); | |
541 blockstart = pos; | |
542 continue; | |
543 } | |
544 continue; | |
545 } | |
546 pos++; | |
547 } | |
548 if (pos > blockstart) { | |
549 ei = expansionitem_create_stringlen(buf + blockstart, | |
25 | 550 pos - blockstart); |
19 | 551 expansionitemarray_add(&m->expansion, ei, NULL); |
552 } | |
553 } | |
554 | |
18 | 555 void |
556 macro_define_plain(struct place *p1, const char *macro, | |
557 struct place *p2, const char *expansion) | |
558 { | |
559 struct macro *m; | |
19 | 560 struct expansionitem *ei; |
18 | 561 |
19 | 562 m = macro_define_common_start(p1, macro, p2); |
563 ei = expansionitem_create_string(expansion); | |
564 expansionitemarray_add(&m->expansion, ei, NULL); | |
18 | 565 macro_define_common_end(m); |
566 } | |
567 | |
568 void | |
569 macro_define_params(struct place *p1, const char *macro, | |
570 struct place *p2, const char *params, | |
571 struct place *p3, const char *expansion) | |
572 { | |
573 struct macro *m; | |
574 | |
19 | 575 m = macro_define_common_start(p1, macro, p3); |
25 | 576 m->hasparams = true; |
18 | 577 macro_parse_parameters(m, p2, params); |
19 | 578 macro_parse_expansion(m, expansion); |
18 | 579 macro_define_common_end(m); |
17 | 580 } |
581 | |
582 void | |
583 macro_undef(const char *macro) | |
584 { | |
585 struct macro *m; | |
586 | |
587 m = macrotable_find(macro, true); | |
588 if (m) { | |
589 macro_destroy(m); | |
590 } | |
591 } | |
592 | |
593 bool | |
594 macro_isdefined(const char *macro) | |
595 { | |
596 struct macro *m; | |
597 | |
598 m = macrotable_find(macro, false); | |
599 return m != NULL; | |
600 } | |
601 | |
602 //////////////////////////////////////////////////////////// | |
603 // macro expansion | |
604 | |
19 | 605 struct expstate { |
606 bool honordefined; | |
607 enum { ES_NORMAL, ES_WANTLPAREN, ES_NOARG, ES_HAVEARG } state; | |
608 struct macro *curmacro; | |
609 struct stringarray args; | |
610 unsigned argparens; | |
611 | |
612 bool tobuf; | |
613 char *buf; | |
614 size_t bufpos, bufmax; | |
615 }; | |
616 | |
617 static struct expstate mainstate; | |
618 | |
619 static void doexpand(struct expstate *es, struct place *p, | |
620 char *buf, size_t len); | |
621 | |
622 static | |
623 void | |
624 expstate_init(struct expstate *es, bool tobuf, bool honordefined) | |
625 { | |
626 es->honordefined = honordefined; | |
627 es->state = ES_NORMAL; | |
628 es->curmacro = NULL; | |
629 stringarray_init(&es->args); | |
630 es->argparens = 0; | |
631 es->tobuf = tobuf; | |
632 es->buf = NULL; | |
633 es->bufpos = 0; | |
634 es->bufmax = 0; | |
635 } | |
636 | |
637 static | |
638 void | |
639 expstate_cleanup(struct expstate *es) | |
640 { | |
641 assert(es->state == ES_NORMAL); | |
642 stringarray_cleanup(&es->args); | |
643 if (es->buf) { | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
644 dofree(es->buf, es->bufmax); |
19 | 645 } |
646 } | |
647 | |
648 static | |
649 void | |
650 expstate_destroyargs(struct expstate *es) | |
651 { | |
652 unsigned i, num; | |
653 | |
654 num = stringarray_num(&es->args); | |
655 for (i=0; i<num; i++) { | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
656 dostrfree(stringarray_get(&es->args, i)); |
19 | 657 } |
658 stringarray_setsize(&es->args, 0); | |
659 } | |
660 | |
661 static | |
662 void | |
21 | 663 expand_send(struct expstate *es, struct place *p, const char *buf, size_t len) |
19 | 664 { |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
665 size_t oldmax; |
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
666 |
19 | 667 if (es->tobuf) { |
38
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
33
diff
changeset
|
668 assert(es->bufpos <= es->bufmax); |
19 | 669 if (es->bufpos + len > es->bufmax) { |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
670 oldmax = es->bufmax; |
19 | 671 if (es->bufmax == 0) { |
672 es->bufmax = 64; | |
673 } | |
674 while (es->bufpos + len > es->bufmax) { | |
675 es->bufmax *= 2; | |
676 } | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
677 es->buf = dorealloc(es->buf, oldmax, es->bufmax); |
19 | 678 } |
679 memcpy(es->buf + es->bufpos, buf, len); | |
680 es->bufpos += len; | |
38
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
33
diff
changeset
|
681 assert(es->bufpos <= es->bufmax); |
19 | 682 } else { |
21 | 683 output(p, buf, len); |
19 | 684 } |
685 } | |
686 | |
687 static | |
688 void | |
21 | 689 expand_send_eof(struct expstate *es, struct place *p) |
19 | 690 { |
691 if (es->tobuf) { | |
21 | 692 expand_send(es, p, "", 1); |
19 | 693 es->bufpos--; |
694 } else { | |
695 output_eof(); | |
696 } | |
697 } | |
698 | |
699 static | |
700 void | |
701 expand_newarg(struct expstate *es, char *buf, size_t len) | |
702 { | |
703 char *text; | |
704 | |
705 text = dostrndup(buf, len); | |
706 stringarray_add(&es->args, text, NULL); | |
707 } | |
708 | |
709 static | |
710 void | |
711 expand_appendarg(struct expstate *es, char *buf, size_t len) | |
712 { | |
713 unsigned num; | |
714 char *text; | |
715 size_t oldlen; | |
716 | |
717 num = stringarray_num(&es->args); | |
718 assert(num > 0); | |
719 | |
720 text = stringarray_get(&es->args, num - 1); | |
721 oldlen = strlen(text); | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
722 text = dorealloc(text, oldlen + 1, oldlen + len + 1); |
19 | 723 memcpy(text + oldlen, buf, len); |
724 text[oldlen+len] = '\0'; | |
725 stringarray_set(&es->args, num - 1, text); | |
726 } | |
727 | |
728 static | |
729 char * | |
28 | 730 expand_substitute(struct place *p, struct expstate *es) |
19 | 731 { |
732 struct expansionitem *ei; | |
733 unsigned i, num; | |
734 size_t len; | |
735 char *arg; | |
736 char *ret; | |
28 | 737 unsigned numargs, numparams; |
738 | |
739 numargs = stringarray_num(&es->args); | |
740 numparams = stringarray_num(&es->curmacro->params); | |
741 | |
742 if (numargs == 0 && numparams == 1) { | |
743 /* no arguments <=> one empty argument */ | |
744 stringarray_add(&es->args, dostrdup(""), NULL); | |
745 numargs++; | |
746 } | |
747 if (numargs != numparams) { | |
748 complain(p, "Wrong number of arguments for macro %s; " | |
749 "found %u, expected %u", | |
750 es->curmacro->name, numargs, numparams); | |
751 complain_fail(); | |
752 while (numargs < numparams) { | |
753 stringarray_add(&es->args, dostrdup(""), NULL); | |
754 numargs++; | |
755 } | |
756 } | |
19 | 757 |
758 len = 0; | |
759 num = expansionitemarray_num(&es->curmacro->expansion); | |
760 for (i=0; i<num; i++) { | |
761 ei = expansionitemarray_get(&es->curmacro->expansion, i); | |
762 if (ei->isstring) { | |
763 len += strlen(ei->string); | |
764 } else { | |
765 arg = stringarray_get(&es->args, ei->param); | |
766 len += strlen(arg); | |
767 } | |
768 } | |
769 | |
770 ret = domalloc(len+1); | |
771 *ret = '\0'; | |
772 for (i=0; i<num; i++) { | |
773 ei = expansionitemarray_get(&es->curmacro->expansion, i); | |
774 if (ei->isstring) { | |
775 strcat(ret, ei->string); | |
776 } else { | |
777 arg = stringarray_get(&es->args, ei->param); | |
778 strcat(ret, arg); | |
779 } | |
780 } | |
781 | |
782 return ret; | |
783 } | |
784 | |
785 static | |
786 void | |
787 expand_domacro(struct expstate *es, struct place *p) | |
788 { | |
789 struct macro *m; | |
790 char *newbuf, *newbuf2; | |
791 | |
792 if (es->curmacro == NULL) { | |
793 /* defined() */ | |
794 if (stringarray_num(&es->args) != 1) { | |
795 complain(p, "Too many arguments for defined()"); | |
796 complain_fail(); | |
21 | 797 expand_send(es, p, "0", 1); |
19 | 798 return; |
799 } | |
800 m = macrotable_find(stringarray_get(&es->args, 0), false); | |
21 | 801 expand_send(es, p, (m != NULL) ? "1" : "0", 1); |
25 | 802 expstate_destroyargs(es); |
19 | 803 return; |
804 } | |
805 | |
806 assert(es->curmacro->inuse == false); | |
807 es->curmacro->inuse = true; | |
808 | |
28 | 809 newbuf = expand_substitute(p, es); |
19 | 810 newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false); |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
811 dostrfree(newbuf); |
25 | 812 expstate_destroyargs(es); |
19 | 813 doexpand(es, p, newbuf2, strlen(newbuf2)); |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
814 dostrfree(newbuf2); |
19 | 815 |
816 es->curmacro->inuse = false; | |
817 } | |
818 | |
819 static | |
820 void | |
821 expand_got_ws(struct expstate *es, struct place *p, char *buf, size_t len) | |
822 { | |
823 switch (es->state) { | |
824 case ES_NORMAL: | |
21 | 825 expand_send(es, p, buf, len); |
19 | 826 break; |
827 case ES_WANTLPAREN: | |
828 break; | |
829 case ES_NOARG: | |
830 break; | |
831 case ES_HAVEARG: | |
832 expand_appendarg(es, buf, len); | |
833 break; | |
834 } | |
835 } | |
836 | |
837 static | |
838 void | |
839 expand_got_word(struct expstate *es, struct place *p, char *buf, size_t len) | |
840 { | |
841 struct macro *m; | |
842 struct expansionitem *ei; | |
843 char *newbuf; | |
17 | 844 |
19 | 845 switch (es->state) { |
846 case ES_NORMAL: | |
847 if (es->honordefined && | |
848 len == 7 && !memcmp(buf, "defined", 7)) { | |
849 es->curmacro = NULL; | |
850 es->state = ES_WANTLPAREN; | |
851 break; | |
852 } | |
853 m = macrotable_findlen(buf, len, false); | |
42
ad7763329eba
Don't crash if a macro tries to expand itself recursively.
David A. Holland
parents:
40
diff
changeset
|
854 if (m == NULL || m->inuse) { |
21 | 855 expand_send(es, p, buf, len); |
19 | 856 } else if (!m->hasparams) { |
857 m->inuse = true; | |
858 assert(expansionitemarray_num(&m->expansion) == 1); | |
859 ei = expansionitemarray_get(&m->expansion, 0); | |
860 assert(ei->isstring); | |
861 newbuf = macroexpand(p, ei->string, | |
862 strlen(ei->string), false); | |
863 doexpand(es, p, newbuf, strlen(newbuf)); | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
864 dostrfree(newbuf); |
19 | 865 m->inuse = false; |
866 } else { | |
867 es->curmacro = m; | |
868 es->state = ES_WANTLPAREN; | |
869 } | |
870 break; | |
871 case ES_WANTLPAREN: | |
872 if (es->curmacro != NULL) { | |
873 complain(p, "Expected arguments for macro %s", | |
874 es->curmacro->name); | |
875 complain_fail(); | |
876 } else { | |
877 /* "defined foo" means "defined(foo)" */ | |
878 expand_newarg(es, buf, len); | |
879 es->state = ES_NORMAL; | |
880 expand_domacro(es, p); | |
881 break; | |
882 } | |
883 break; | |
884 case ES_NOARG: | |
885 expand_newarg(es, buf, len); | |
886 es->state = ES_HAVEARG; | |
887 break; | |
888 case ES_HAVEARG: | |
889 expand_appendarg(es, buf, len); | |
890 break; | |
891 } | |
892 } | |
893 | |
894 static | |
895 void | |
896 expand_got_lparen(struct expstate *es, struct place *p, char *buf, size_t len) | |
897 { | |
898 switch (es->state) { | |
899 case ES_NORMAL: | |
21 | 900 expand_send(es, p, buf, len); |
19 | 901 break; |
902 case ES_WANTLPAREN: | |
903 es->state = ES_NOARG; | |
904 break; | |
905 case ES_NOARG: | |
906 expand_newarg(es, buf, len); | |
907 es->state = ES_HAVEARG; | |
908 es->argparens++; | |
909 break; | |
910 case ES_HAVEARG: | |
911 expand_appendarg(es, buf, len); | |
912 es->argparens++; | |
913 break; | |
914 } | |
915 } | |
916 | |
917 static | |
918 void | |
919 expand_got_rparen(struct expstate *es, struct place *p, char *buf, size_t len) | |
920 { | |
921 switch (es->state) { | |
922 case ES_NORMAL: | |
21 | 923 expand_send(es, p, buf, len); |
19 | 924 break; |
925 case ES_WANTLPAREN: | |
926 if (es->curmacro) { | |
927 complain(p, "Expected arguments for macro %s", | |
928 es->curmacro->name); | |
929 } else { | |
930 complain(p, "Expected arguments for defined()"); | |
931 } | |
932 complain_fail(); | |
933 break; | |
934 case ES_NOARG: | |
935 assert(es->argparens == 0); | |
936 es->state = ES_NORMAL; | |
937 expand_domacro(es, p); | |
938 break; | |
939 case ES_HAVEARG: | |
940 if (es->argparens > 0) { | |
941 es->argparens--; | |
942 expand_appendarg(es, buf, len); | |
943 } else { | |
944 es->state = ES_NORMAL; | |
945 expand_domacro(es, p); | |
946 } | |
947 break; | |
948 } | |
949 } | |
950 | |
951 static | |
952 void | |
953 expand_got_comma(struct expstate *es, struct place *p, char *buf, size_t len) | |
954 { | |
955 switch (es->state) { | |
956 case ES_NORMAL: | |
21 | 957 expand_send(es, p, buf, len); |
19 | 958 break; |
959 case ES_WANTLPAREN: | |
960 if (es->curmacro) { | |
961 complain(p, "Expected arguments for macro %s", | |
962 es->curmacro->name); | |
963 } else { | |
964 complain(p, "Expected arguments for defined()"); | |
965 } | |
966 complain_fail(); | |
967 break; | |
968 case ES_NOARG: | |
969 assert(es->argparens == 0); | |
970 expand_newarg(es, buf, 0); | |
971 break; | |
972 case ES_HAVEARG: | |
973 if (es->argparens > 0) { | |
974 expand_appendarg(es, buf, len); | |
975 } else { | |
976 es->state = ES_NOARG; | |
977 } | |
978 break; | |
979 } | |
980 } | |
981 | |
982 static | |
983 void | |
984 expand_got_other(struct expstate *es, struct place *p, char *buf, size_t len) | |
985 { | |
986 switch (es->state) { | |
987 case ES_NORMAL: | |
21 | 988 expand_send(es, p, buf, len); |
19 | 989 break; |
990 case ES_WANTLPAREN: | |
991 if (es->curmacro) { | |
992 complain(p, "Expected arguments for macro %s", | |
993 es->curmacro->name); | |
994 } else { | |
995 complain(p, "Expected arguments for defined()"); | |
996 } | |
997 complain_fail(); | |
998 break; | |
999 case ES_NOARG: | |
1000 expand_newarg(es, buf, len); | |
1001 es->state = ES_HAVEARG; | |
1002 break; | |
1003 case ES_HAVEARG: | |
1004 expand_appendarg(es, buf, len); | |
1005 break; | |
1006 } | |
1007 } | |
1008 | |
1009 static | |
1010 void | |
1011 expand_got_eof(struct expstate *es, struct place *p) | |
1012 { | |
1013 switch (es->state) { | |
1014 case ES_NORMAL: | |
21 | 1015 expand_send_eof(es, p); |
19 | 1016 break; |
1017 case ES_WANTLPAREN: | |
1018 if (es->curmacro) { | |
1019 complain(p, "Expected arguments for macro %s", | |
1020 es->curmacro->name); | |
1021 } else { | |
1022 complain(p, "Expected arguments for defined()"); | |
1023 } | |
1024 complain_fail(); | |
1025 break; | |
1026 case ES_NOARG: | |
1027 case ES_HAVEARG: | |
1028 if (es->curmacro) { | |
1029 complain(p, "Unclosed argument list for macro %s", | |
1030 es->curmacro->name); | |
1031 } else { | |
1032 complain(p, "Unclosed argument list for defined()"); | |
1033 } | |
1034 complain_fail(); | |
1035 expstate_destroyargs(es); | |
1036 break; | |
1037 } | |
1038 es->state = ES_NORMAL; | |
1039 es->curmacro = NULL; | |
1040 es->argparens = 0; | |
1041 } | |
1042 | |
1043 static | |
1044 void | |
1045 doexpand(struct expstate *es, struct place *p, char *buf, size_t len) | |
1046 { | |
1047 size_t x; | |
1048 | |
1049 while (len > 0) { | |
1050 x = strspn(buf, ws); | |
28 | 1051 if (x > len) { |
1052 /* XXX gross, need strnspn */ | |
1053 x = len; | |
1054 } | |
1055 | |
19 | 1056 if (x > 0) { |
1057 expand_got_ws(es, p, buf, x); | |
1058 buf += x; | |
1059 len -= x; | |
28 | 1060 continue; |
19 | 1061 } |
1062 | |
1063 x = strspn(buf, alnum); | |
28 | 1064 if (x > len) { |
1065 /* XXX gross, need strnspn */ | |
1066 x = len; | |
1067 } | |
1068 | |
19 | 1069 if (x > 0) { |
1070 expand_got_word(es, p, buf, x); | |
1071 buf += x; | |
1072 len -= x; | |
1073 continue; | |
1074 } | |
1075 | |
1076 if (buf[0] == '(') { | |
1077 expand_got_lparen(es, p, buf, 1); | |
1078 buf++; | |
1079 len--; | |
1080 continue; | |
1081 } | |
1082 | |
1083 if (buf[0] == ')') { | |
1084 expand_got_rparen(es, p, buf, 1); | |
1085 buf++; | |
1086 len--; | |
1087 continue; | |
1088 } | |
1089 | |
1090 if (buf[0] == ',') { | |
1091 expand_got_comma(es, p, buf, 1); | |
1092 buf++; | |
1093 len--; | |
1094 continue; | |
1095 } | |
1096 | |
1097 expand_got_other(es, p, buf, 1); | |
1098 buf++; | |
1099 len--; | |
1100 } | |
1101 } | |
1102 | |
1103 char * | |
1104 macroexpand(struct place *p, char *buf, size_t len, bool honordefined) | |
1105 { | |
1106 struct expstate es; | |
1107 char *ret; | |
1108 | |
1109 expstate_init(&es, true, honordefined); | |
1110 doexpand(&es, p, buf, len); | |
1111 expand_got_eof(&es, p); | |
40 | 1112 |
1113 /* trim to fit, so the malloc debugging won't complain */ | |
1114 es.buf = dorealloc(es.buf, es.bufmax, strlen(es.buf) + 1); | |
1115 | |
19 | 1116 ret = es.buf; |
1117 es.buf = NULL; | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
1118 es.bufpos = es.bufmax = 0; |
40 | 1119 |
19 | 1120 expstate_cleanup(&es); |
1121 | |
1122 return ret; | |
1123 } | |
1124 | |
1125 void | |
1126 macro_sendline(struct place *p, char *buf, size_t len) | |
1127 { | |
1128 doexpand(&mainstate, p, buf, len); | |
21 | 1129 output(p, "\n", 1); |
19 | 1130 } |
1131 | |
1132 void | |
1133 macro_sendeof(struct place *p) | |
1134 { | |
1135 expand_got_eof(&mainstate, p); | |
1136 } | |
17 | 1137 |
1138 //////////////////////////////////////////////////////////// | |
1139 // module initialization | |
1140 | |
1141 void | |
1142 macros_init(void) | |
1143 { | |
1144 macrotable_init(); | |
19 | 1145 expstate_init(&mainstate, false, false); |
17 | 1146 } |
1147 | |
1148 void | |
1149 macros_cleanup(void) | |
1150 { | |
19 | 1151 expstate_cleanup(&mainstate); |
17 | 1152 macrotable_cleanup(); |
1153 } |