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 DECLARRAY(expansionitem);
|
|
48 DEFARRAY(expansionitem, );
|
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 };
|
|
60 DECLARRAY(macro);
|
|
61 DEFARRAY(macro, );
|
|
62 DECLARRAY(macroarray);
|
|
63 DEFARRAY(macroarray, );
|
|
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) {
|
|
113 free(ei->string);
|
|
114 }
|
|
115 free(ei);
|
|
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);
|
17
|
165 free(m->name);
|
|
166 free(m);
|
|
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;
|
|
358 tossbit = newmask && ~hashmask;
|
|
359 hashmask = newmask;
|
|
360
|
|
361 for (i=0; i<numbuckets; i++) {
|
|
362 newbucket = NULL;
|
|
363 oldbucket = macroarrayarray_get(¯os, i);
|
|
364 oldnum = macroarray_num(oldbucket);
|
|
365 for (j=0; j<oldnum; j++) {
|
|
366 m = macroarray_get(oldbucket, j);
|
|
367 if (m->hash & tossbit) {
|
|
368 if (newbucket == NULL) {
|
|
369 newbucket = macroarray_create();
|
|
370 }
|
|
371 macroarray_set(oldbucket, j, NULL);
|
|
372 macroarray_add(newbucket, m, NULL);
|
|
373 }
|
|
374 }
|
|
375 for (j=k=0; j<oldnum; j++) {
|
|
376 m = macroarray_get(oldbucket, j);
|
|
377 if (m != NULL && k < j) {
|
|
378 macroarray_set(oldbucket, k++, m);
|
|
379 }
|
|
380 }
|
|
381 macroarray_setsize(oldbucket, k);
|
|
382 macroarrayarray_set(¯os, numbuckets + i, newbucket);
|
|
383 }
|
|
384 }
|
|
385
|
|
386 static
|
|
387 void
|
|
388 macrotable_add(struct macro *m)
|
|
389 {
|
|
390 unsigned hash;
|
|
391 struct macroarray *bucket;
|
|
392 unsigned numbuckets;
|
|
393
|
|
394 numbuckets = macroarrayarray_num(¯os);
|
|
395 if (total_macros > 0 && total_macros / numbuckets > 9) {
|
|
396 macrotable_rehash();
|
|
397 }
|
|
398
|
19
|
399 hash = hashfunc(m->name, strlen(m->name));
|
17
|
400 bucket = macroarrayarray_get(¯os, hash & hashmask);
|
|
401 if (bucket == NULL) {
|
|
402 bucket = macroarray_create();
|
|
403 macroarrayarray_set(¯os, hash & hashmask, bucket);
|
|
404 }
|
|
405 macroarray_add(bucket, m, NULL);
|
|
406 total_macros++;
|
|
407 }
|
|
408
|
|
409 ////////////////////////////////////////////////////////////
|
|
410 // external macro definition interface
|
|
411
|
18
|
412 static
|
|
413 struct macro *
|
|
414 macro_define_common_start(struct place *p1, const char *macro,
|
19
|
415 struct place *p2)
|
17
|
416 {
|
|
417 struct macro *m;
|
19
|
418 unsigned hash;
|
17
|
419
|
18
|
420 if (!is_identifier(macro)) {
|
|
421 complain(p1, "Invalid macro name %s", macro);
|
|
422 complain_fail();
|
|
423 }
|
|
424
|
19
|
425 hash = hashfunc(macro, strlen(macro));
|
|
426 m = macro_create(p1, macro, hash, p2);
|
18
|
427 return m;
|
|
428 }
|
|
429
|
|
430 static
|
|
431 void
|
|
432 macro_define_common_end(struct macro *m)
|
|
433 {
|
|
434 struct macro *oldm;
|
|
435 bool ok;
|
|
436
|
|
437 oldm = macrotable_find(m->name, false);
|
|
438 if (oldm != NULL) {
|
|
439 ok = macro_eq(m, oldm);
|
|
440 if (ok) {
|
|
441 complain(&m->defplace,
|
|
442 "Warning: redefinition of %s", m->name);
|
17
|
443 if (mode.werror) {
|
|
444 complain_fail();
|
|
445 }
|
18
|
446 } else {
|
|
447 complain(&m->defplace,
|
|
448 "Redefinition of %s is not identical",
|
|
449 m->name);
|
|
450 complain_fail();
|
17
|
451 }
|
18
|
452 complain(&oldm->defplace, "Previous definition was here");
|
|
453 macro_destroy(m);
|
17
|
454 return;
|
|
455 }
|
18
|
456 macrotable_add(m);
|
|
457 }
|
17
|
458
|
18
|
459 static
|
|
460 void
|
|
461 macro_parse_parameters(struct macro *m, struct place *p, const char *params)
|
|
462 {
|
|
463 size_t len;
|
|
464 const char *s;
|
|
465 char *param;
|
|
466
|
|
467 while (params != NULL) {
|
|
468 len = strspn(params, ws);
|
|
469 params += len;
|
|
470 p->column += len;
|
|
471 s = strchr(params, ',');
|
|
472 if (s) {
|
|
473 len = s-params;
|
|
474 param = dostrndup(params, len);
|
|
475 s++;
|
|
476 } else {
|
|
477 len = strlen(params);
|
|
478 param = dostrndup(params, len);
|
|
479 }
|
|
480 notrailingws(param, strlen(param));
|
|
481 if (!is_identifier(param)) {
|
|
482 complain(p, "Invalid macro parameter name %s", param);
|
|
483 complain_fail();
|
|
484 } else {
|
|
485 stringarray_add(&m->params, param, NULL);
|
|
486 }
|
|
487 params = s;
|
|
488 p->column += len;
|
|
489 }
|
|
490 }
|
|
491
|
19
|
492 static
|
|
493 bool
|
|
494 isparam(struct macro *m, const char *name, size_t len, unsigned *num_ret)
|
|
495 {
|
|
496 unsigned num, i;
|
|
497 const char *param;
|
|
498
|
|
499 num = stringarray_num(&m->params);
|
|
500 for (i=0; i<num; i++) {
|
|
501 param = stringarray_get(&m->params, i);
|
27
|
502 if (strlen(param) == len && !memcmp(name, param, len)) {
|
19
|
503 *num_ret = i;
|
|
504 return true;
|
|
505 }
|
|
506 }
|
|
507 return false;
|
|
508 }
|
|
509
|
|
510 static
|
|
511 void
|
|
512 macro_parse_expansion(struct macro *m, const char *buf)
|
|
513 {
|
|
514 size_t blockstart, wordstart, pos;
|
|
515 struct expansionitem *ei;
|
|
516 unsigned param;
|
|
517
|
|
518 pos = blockstart = 0;
|
|
519 while (buf[pos] != '\0') {
|
|
520 pos += strspn(buf+pos, ws);
|
|
521 if (strchr(alnum, buf[pos])) {
|
|
522 wordstart = pos;
|
|
523 pos += strspn(buf+pos, alnum);
|
|
524 if (isparam(m, buf+wordstart, pos-wordstart, ¶m)) {
|
|
525 if (pos > blockstart) {
|
|
526 ei = expansionitem_create_stringlen(
|
|
527 buf + blockstart,
|
27
|
528 wordstart - blockstart);
|
19
|
529 expansionitemarray_add(&m->expansion,
|
|
530 ei, NULL);
|
|
531 }
|
|
532 ei = expansionitem_create_param(param);
|
|
533 expansionitemarray_add(&m->expansion, ei,NULL);
|
|
534 blockstart = pos;
|
|
535 continue;
|
|
536 }
|
|
537 continue;
|
|
538 }
|
|
539 pos++;
|
|
540 }
|
|
541 if (pos > blockstart) {
|
|
542 ei = expansionitem_create_stringlen(buf + blockstart,
|
25
|
543 pos - blockstart);
|
19
|
544 expansionitemarray_add(&m->expansion, ei, NULL);
|
|
545 }
|
|
546 }
|
|
547
|
18
|
548 void
|
|
549 macro_define_plain(struct place *p1, const char *macro,
|
|
550 struct place *p2, const char *expansion)
|
|
551 {
|
|
552 struct macro *m;
|
19
|
553 struct expansionitem *ei;
|
18
|
554
|
19
|
555 m = macro_define_common_start(p1, macro, p2);
|
|
556 ei = expansionitem_create_string(expansion);
|
|
557 expansionitemarray_add(&m->expansion, ei, NULL);
|
18
|
558 macro_define_common_end(m);
|
|
559 }
|
|
560
|
|
561 void
|
|
562 macro_define_params(struct place *p1, const char *macro,
|
|
563 struct place *p2, const char *params,
|
|
564 struct place *p3, const char *expansion)
|
|
565 {
|
|
566 struct macro *m;
|
|
567
|
19
|
568 m = macro_define_common_start(p1, macro, p3);
|
25
|
569 m->hasparams = true;
|
18
|
570 macro_parse_parameters(m, p2, params);
|
19
|
571 macro_parse_expansion(m, expansion);
|
18
|
572 macro_define_common_end(m);
|
17
|
573 }
|
|
574
|
|
575 void
|
|
576 macro_undef(const char *macro)
|
|
577 {
|
|
578 struct macro *m;
|
|
579
|
|
580 m = macrotable_find(macro, true);
|
|
581 if (m) {
|
|
582 macro_destroy(m);
|
|
583 }
|
|
584 }
|
|
585
|
|
586 bool
|
|
587 macro_isdefined(const char *macro)
|
|
588 {
|
|
589 struct macro *m;
|
|
590
|
|
591 m = macrotable_find(macro, false);
|
|
592 return m != NULL;
|
|
593 }
|
|
594
|
|
595 ////////////////////////////////////////////////////////////
|
|
596 // macro expansion
|
|
597
|
19
|
598 struct expstate {
|
|
599 bool honordefined;
|
|
600 enum { ES_NORMAL, ES_WANTLPAREN, ES_NOARG, ES_HAVEARG } state;
|
|
601 struct macro *curmacro;
|
|
602 struct stringarray args;
|
|
603 unsigned argparens;
|
|
604
|
|
605 bool tobuf;
|
|
606 char *buf;
|
|
607 size_t bufpos, bufmax;
|
|
608 };
|
|
609
|
|
610 static struct expstate mainstate;
|
|
611
|
|
612 static void doexpand(struct expstate *es, struct place *p,
|
|
613 char *buf, size_t len);
|
|
614
|
|
615 static
|
|
616 void
|
|
617 expstate_init(struct expstate *es, bool tobuf, bool honordefined)
|
|
618 {
|
|
619 es->honordefined = honordefined;
|
|
620 es->state = ES_NORMAL;
|
|
621 es->curmacro = NULL;
|
|
622 stringarray_init(&es->args);
|
|
623 es->argparens = 0;
|
|
624 es->tobuf = tobuf;
|
|
625 es->buf = NULL;
|
|
626 es->bufpos = 0;
|
|
627 es->bufmax = 0;
|
|
628 }
|
|
629
|
|
630 static
|
|
631 void
|
|
632 expstate_cleanup(struct expstate *es)
|
|
633 {
|
|
634 assert(es->state == ES_NORMAL);
|
|
635 stringarray_cleanup(&es->args);
|
|
636 if (es->buf) {
|
|
637 free(es->buf);
|
|
638 }
|
|
639 }
|
|
640
|
|
641 static
|
|
642 void
|
|
643 expstate_destroyargs(struct expstate *es)
|
|
644 {
|
|
645 unsigned i, num;
|
|
646
|
|
647 num = stringarray_num(&es->args);
|
|
648 for (i=0; i<num; i++) {
|
|
649 free(stringarray_get(&es->args, i));
|
|
650 }
|
|
651 stringarray_setsize(&es->args, 0);
|
|
652 }
|
|
653
|
|
654 static
|
|
655 void
|
21
|
656 expand_send(struct expstate *es, struct place *p, const char *buf, size_t len)
|
19
|
657 {
|
|
658 if (es->tobuf) {
|
|
659 if (es->bufpos + len > es->bufmax) {
|
|
660 if (es->bufmax == 0) {
|
|
661 es->bufmax = 64;
|
|
662 }
|
|
663 while (es->bufpos + len > es->bufmax) {
|
|
664 es->bufmax *= 2;
|
|
665 }
|
|
666 es->buf = dorealloc(es->buf, es->bufmax);
|
|
667 }
|
|
668 memcpy(es->buf + es->bufpos, buf, len);
|
|
669 es->bufpos += len;
|
|
670 } else {
|
21
|
671 output(p, buf, len);
|
19
|
672 }
|
|
673 }
|
|
674
|
|
675 static
|
|
676 void
|
21
|
677 expand_send_eof(struct expstate *es, struct place *p)
|
19
|
678 {
|
|
679 if (es->tobuf) {
|
21
|
680 expand_send(es, p, "", 1);
|
19
|
681 es->bufpos--;
|
|
682 } else {
|
|
683 output_eof();
|
|
684 }
|
|
685 }
|
|
686
|
|
687 static
|
|
688 void
|
|
689 expand_newarg(struct expstate *es, char *buf, size_t len)
|
|
690 {
|
|
691 char *text;
|
|
692
|
|
693 text = dostrndup(buf, len);
|
|
694 stringarray_add(&es->args, text, NULL);
|
|
695 }
|
|
696
|
|
697 static
|
|
698 void
|
|
699 expand_appendarg(struct expstate *es, char *buf, size_t len)
|
|
700 {
|
|
701 unsigned num;
|
|
702 char *text;
|
|
703 size_t oldlen;
|
|
704
|
|
705 num = stringarray_num(&es->args);
|
|
706 assert(num > 0);
|
|
707
|
|
708 text = stringarray_get(&es->args, num - 1);
|
|
709 oldlen = strlen(text);
|
|
710 text = dorealloc(text, oldlen + len + 1);
|
|
711 memcpy(text + oldlen, buf, len);
|
|
712 text[oldlen+len] = '\0';
|
|
713 stringarray_set(&es->args, num - 1, text);
|
|
714 }
|
|
715
|
|
716 static
|
|
717 char *
|
28
|
718 expand_substitute(struct place *p, struct expstate *es)
|
19
|
719 {
|
|
720 struct expansionitem *ei;
|
|
721 unsigned i, num;
|
|
722 size_t len;
|
|
723 char *arg;
|
|
724 char *ret;
|
28
|
725 unsigned numargs, numparams;
|
|
726
|
|
727 numargs = stringarray_num(&es->args);
|
|
728 numparams = stringarray_num(&es->curmacro->params);
|
|
729
|
|
730 if (numargs == 0 && numparams == 1) {
|
|
731 /* no arguments <=> one empty argument */
|
|
732 stringarray_add(&es->args, dostrdup(""), NULL);
|
|
733 numargs++;
|
|
734 }
|
|
735 if (numargs != numparams) {
|
|
736 complain(p, "Wrong number of arguments for macro %s; "
|
|
737 "found %u, expected %u",
|
|
738 es->curmacro->name, numargs, numparams);
|
|
739 complain_fail();
|
|
740 while (numargs < numparams) {
|
|
741 stringarray_add(&es->args, dostrdup(""), NULL);
|
|
742 numargs++;
|
|
743 }
|
|
744 }
|
19
|
745
|
|
746 len = 0;
|
|
747 num = expansionitemarray_num(&es->curmacro->expansion);
|
|
748 for (i=0; i<num; i++) {
|
|
749 ei = expansionitemarray_get(&es->curmacro->expansion, i);
|
|
750 if (ei->isstring) {
|
|
751 len += strlen(ei->string);
|
|
752 } else {
|
|
753 arg = stringarray_get(&es->args, ei->param);
|
|
754 len += strlen(arg);
|
|
755 }
|
|
756 }
|
|
757
|
|
758 ret = domalloc(len+1);
|
|
759 *ret = '\0';
|
|
760 for (i=0; i<num; i++) {
|
|
761 ei = expansionitemarray_get(&es->curmacro->expansion, i);
|
|
762 if (ei->isstring) {
|
|
763 strcat(ret, ei->string);
|
|
764 } else {
|
|
765 arg = stringarray_get(&es->args, ei->param);
|
|
766 strcat(ret, arg);
|
|
767 }
|
|
768 }
|
|
769
|
|
770 return ret;
|
|
771 }
|
|
772
|
|
773 static
|
|
774 void
|
|
775 expand_domacro(struct expstate *es, struct place *p)
|
|
776 {
|
|
777 struct macro *m;
|
|
778 char *newbuf, *newbuf2;
|
|
779
|
|
780 if (es->curmacro == NULL) {
|
|
781 /* defined() */
|
|
782 if (stringarray_num(&es->args) != 1) {
|
|
783 complain(p, "Too many arguments for defined()");
|
|
784 complain_fail();
|
21
|
785 expand_send(es, p, "0", 1);
|
19
|
786 return;
|
|
787 }
|
|
788 m = macrotable_find(stringarray_get(&es->args, 0), false);
|
21
|
789 expand_send(es, p, (m != NULL) ? "1" : "0", 1);
|
25
|
790 expstate_destroyargs(es);
|
19
|
791 return;
|
|
792 }
|
|
793
|
|
794 assert(es->curmacro->inuse == false);
|
|
795 es->curmacro->inuse = true;
|
|
796
|
28
|
797 newbuf = expand_substitute(p, es);
|
19
|
798 newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false);
|
|
799 free(newbuf);
|
25
|
800 expstate_destroyargs(es);
|
19
|
801 doexpand(es, p, newbuf2, strlen(newbuf2));
|
|
802 free(newbuf2);
|
|
803
|
|
804 es->curmacro->inuse = false;
|
|
805 }
|
|
806
|
|
807 static
|
|
808 void
|
|
809 expand_got_ws(struct expstate *es, struct place *p, char *buf, size_t len)
|
|
810 {
|
|
811 switch (es->state) {
|
|
812 case ES_NORMAL:
|
21
|
813 expand_send(es, p, buf, len);
|
19
|
814 break;
|
|
815 case ES_WANTLPAREN:
|
|
816 break;
|
|
817 case ES_NOARG:
|
|
818 break;
|
|
819 case ES_HAVEARG:
|
|
820 expand_appendarg(es, buf, len);
|
|
821 break;
|
|
822 }
|
|
823 }
|
|
824
|
|
825 static
|
|
826 void
|
|
827 expand_got_word(struct expstate *es, struct place *p, char *buf, size_t len)
|
|
828 {
|
|
829 struct macro *m;
|
|
830 struct expansionitem *ei;
|
|
831 char *newbuf;
|
17
|
832
|
19
|
833 switch (es->state) {
|
|
834 case ES_NORMAL:
|
|
835 if (es->honordefined &&
|
|
836 len == 7 && !memcmp(buf, "defined", 7)) {
|
|
837 es->curmacro = NULL;
|
|
838 es->state = ES_WANTLPAREN;
|
|
839 break;
|
|
840 }
|
|
841 m = macrotable_findlen(buf, len, false);
|
|
842 if (m == NULL) {
|
21
|
843 expand_send(es, p, buf, len);
|
19
|
844 } else if (!m->hasparams) {
|
|
845 m->inuse = true;
|
|
846 assert(expansionitemarray_num(&m->expansion) == 1);
|
|
847 ei = expansionitemarray_get(&m->expansion, 0);
|
|
848 assert(ei->isstring);
|
|
849 newbuf = macroexpand(p, ei->string,
|
|
850 strlen(ei->string), false);
|
|
851 doexpand(es, p, newbuf, strlen(newbuf));
|
|
852 free(newbuf);
|
|
853 m->inuse = false;
|
|
854 } else {
|
|
855 es->curmacro = m;
|
|
856 es->state = ES_WANTLPAREN;
|
|
857 }
|
|
858 break;
|
|
859 case ES_WANTLPAREN:
|
|
860 if (es->curmacro != NULL) {
|
|
861 complain(p, "Expected arguments for macro %s",
|
|
862 es->curmacro->name);
|
|
863 complain_fail();
|
|
864 } else {
|
|
865 /* "defined foo" means "defined(foo)" */
|
|
866 expand_newarg(es, buf, len);
|
|
867 es->state = ES_NORMAL;
|
|
868 expand_domacro(es, p);
|
|
869 break;
|
|
870 }
|
|
871 break;
|
|
872 case ES_NOARG:
|
|
873 expand_newarg(es, buf, len);
|
|
874 es->state = ES_HAVEARG;
|
|
875 break;
|
|
876 case ES_HAVEARG:
|
|
877 expand_appendarg(es, buf, len);
|
|
878 break;
|
|
879 }
|
|
880 }
|
|
881
|
|
882 static
|
|
883 void
|
|
884 expand_got_lparen(struct expstate *es, struct place *p, char *buf, size_t len)
|
|
885 {
|
|
886 switch (es->state) {
|
|
887 case ES_NORMAL:
|
21
|
888 expand_send(es, p, buf, len);
|
19
|
889 break;
|
|
890 case ES_WANTLPAREN:
|
|
891 es->state = ES_NOARG;
|
|
892 break;
|
|
893 case ES_NOARG:
|
|
894 expand_newarg(es, buf, len);
|
|
895 es->state = ES_HAVEARG;
|
|
896 es->argparens++;
|
|
897 break;
|
|
898 case ES_HAVEARG:
|
|
899 expand_appendarg(es, buf, len);
|
|
900 es->argparens++;
|
|
901 break;
|
|
902 }
|
|
903 }
|
|
904
|
|
905 static
|
|
906 void
|
|
907 expand_got_rparen(struct expstate *es, struct place *p, char *buf, size_t len)
|
|
908 {
|
|
909 switch (es->state) {
|
|
910 case ES_NORMAL:
|
21
|
911 expand_send(es, p, buf, len);
|
19
|
912 break;
|
|
913 case ES_WANTLPAREN:
|
|
914 if (es->curmacro) {
|
|
915 complain(p, "Expected arguments for macro %s",
|
|
916 es->curmacro->name);
|
|
917 } else {
|
|
918 complain(p, "Expected arguments for defined()");
|
|
919 }
|
|
920 complain_fail();
|
|
921 break;
|
|
922 case ES_NOARG:
|
|
923 assert(es->argparens == 0);
|
|
924 es->state = ES_NORMAL;
|
|
925 expand_domacro(es, p);
|
|
926 break;
|
|
927 case ES_HAVEARG:
|
|
928 if (es->argparens > 0) {
|
|
929 es->argparens--;
|
|
930 expand_appendarg(es, buf, len);
|
|
931 } else {
|
|
932 es->state = ES_NORMAL;
|
|
933 expand_domacro(es, p);
|
|
934 }
|
|
935 break;
|
|
936 }
|
|
937 }
|
|
938
|
|
939 static
|
|
940 void
|
|
941 expand_got_comma(struct expstate *es, struct place *p, char *buf, size_t len)
|
|
942 {
|
|
943 switch (es->state) {
|
|
944 case ES_NORMAL:
|
21
|
945 expand_send(es, p, buf, len);
|
19
|
946 break;
|
|
947 case ES_WANTLPAREN:
|
|
948 if (es->curmacro) {
|
|
949 complain(p, "Expected arguments for macro %s",
|
|
950 es->curmacro->name);
|
|
951 } else {
|
|
952 complain(p, "Expected arguments for defined()");
|
|
953 }
|
|
954 complain_fail();
|
|
955 break;
|
|
956 case ES_NOARG:
|
|
957 assert(es->argparens == 0);
|
|
958 expand_newarg(es, buf, 0);
|
|
959 break;
|
|
960 case ES_HAVEARG:
|
|
961 if (es->argparens > 0) {
|
|
962 expand_appendarg(es, buf, len);
|
|
963 } else {
|
|
964 es->state = ES_NOARG;
|
|
965 }
|
|
966 break;
|
|
967 }
|
|
968 }
|
|
969
|
|
970 static
|
|
971 void
|
|
972 expand_got_other(struct expstate *es, struct place *p, char *buf, size_t len)
|
|
973 {
|
|
974 switch (es->state) {
|
|
975 case ES_NORMAL:
|
21
|
976 expand_send(es, p, buf, len);
|
19
|
977 break;
|
|
978 case ES_WANTLPAREN:
|
|
979 if (es->curmacro) {
|
|
980 complain(p, "Expected arguments for macro %s",
|
|
981 es->curmacro->name);
|
|
982 } else {
|
|
983 complain(p, "Expected arguments for defined()");
|
|
984 }
|
|
985 complain_fail();
|
|
986 break;
|
|
987 case ES_NOARG:
|
|
988 expand_newarg(es, buf, len);
|
|
989 es->state = ES_HAVEARG;
|
|
990 break;
|
|
991 case ES_HAVEARG:
|
|
992 expand_appendarg(es, buf, len);
|
|
993 break;
|
|
994 }
|
|
995 }
|
|
996
|
|
997 static
|
|
998 void
|
|
999 expand_got_eof(struct expstate *es, struct place *p)
|
|
1000 {
|
|
1001 switch (es->state) {
|
|
1002 case ES_NORMAL:
|
21
|
1003 expand_send_eof(es, p);
|
19
|
1004 break;
|
|
1005 case ES_WANTLPAREN:
|
|
1006 if (es->curmacro) {
|
|
1007 complain(p, "Expected arguments for macro %s",
|
|
1008 es->curmacro->name);
|
|
1009 } else {
|
|
1010 complain(p, "Expected arguments for defined()");
|
|
1011 }
|
|
1012 complain_fail();
|
|
1013 break;
|
|
1014 case ES_NOARG:
|
|
1015 case ES_HAVEARG:
|
|
1016 if (es->curmacro) {
|
|
1017 complain(p, "Unclosed argument list for macro %s",
|
|
1018 es->curmacro->name);
|
|
1019 } else {
|
|
1020 complain(p, "Unclosed argument list for defined()");
|
|
1021 }
|
|
1022 complain_fail();
|
|
1023 expstate_destroyargs(es);
|
|
1024 break;
|
|
1025 }
|
|
1026 es->state = ES_NORMAL;
|
|
1027 es->curmacro = NULL;
|
|
1028 es->argparens = 0;
|
|
1029 }
|
|
1030
|
|
1031 static
|
|
1032 void
|
|
1033 doexpand(struct expstate *es, struct place *p, char *buf, size_t len)
|
|
1034 {
|
|
1035 size_t x;
|
|
1036
|
|
1037 while (len > 0) {
|
|
1038 x = strspn(buf, ws);
|
28
|
1039 if (x > len) {
|
|
1040 /* XXX gross, need strnspn */
|
|
1041 x = len;
|
|
1042 }
|
|
1043
|
19
|
1044 if (x > 0) {
|
|
1045 expand_got_ws(es, p, buf, x);
|
|
1046 buf += x;
|
|
1047 len -= x;
|
28
|
1048 continue;
|
19
|
1049 }
|
|
1050
|
|
1051 x = strspn(buf, alnum);
|
28
|
1052 if (x > len) {
|
|
1053 /* XXX gross, need strnspn */
|
|
1054 x = len;
|
|
1055 }
|
|
1056
|
19
|
1057 if (x > 0) {
|
|
1058 expand_got_word(es, p, buf, x);
|
|
1059 buf += x;
|
|
1060 len -= x;
|
|
1061 continue;
|
|
1062 }
|
|
1063
|
|
1064 if (buf[0] == '(') {
|
|
1065 expand_got_lparen(es, p, buf, 1);
|
|
1066 buf++;
|
|
1067 len--;
|
|
1068 continue;
|
|
1069 }
|
|
1070
|
|
1071 if (buf[0] == ')') {
|
|
1072 expand_got_rparen(es, p, buf, 1);
|
|
1073 buf++;
|
|
1074 len--;
|
|
1075 continue;
|
|
1076 }
|
|
1077
|
|
1078 if (buf[0] == ',') {
|
|
1079 expand_got_comma(es, p, buf, 1);
|
|
1080 buf++;
|
|
1081 len--;
|
|
1082 continue;
|
|
1083 }
|
|
1084
|
|
1085 expand_got_other(es, p, buf, 1);
|
|
1086 buf++;
|
|
1087 len--;
|
|
1088 }
|
|
1089 }
|
|
1090
|
|
1091 char *
|
|
1092 macroexpand(struct place *p, char *buf, size_t len, bool honordefined)
|
|
1093 {
|
|
1094 struct expstate es;
|
|
1095 char *ret;
|
|
1096
|
|
1097 expstate_init(&es, true, honordefined);
|
|
1098 doexpand(&es, p, buf, len);
|
|
1099 expand_got_eof(&es, p);
|
|
1100 ret = es.buf;
|
|
1101 es.buf = NULL;
|
|
1102 expstate_cleanup(&es);
|
|
1103
|
|
1104 return ret;
|
|
1105 }
|
|
1106
|
|
1107 void
|
|
1108 macro_sendline(struct place *p, char *buf, size_t len)
|
|
1109 {
|
|
1110 doexpand(&mainstate, p, buf, len);
|
21
|
1111 output(p, "\n", 1);
|
19
|
1112 }
|
|
1113
|
|
1114 void
|
|
1115 macro_sendeof(struct place *p)
|
|
1116 {
|
|
1117 expand_got_eof(&mainstate, p);
|
|
1118 }
|
17
|
1119
|
|
1120 ////////////////////////////////////////////////////////////
|
|
1121 // module initialization
|
|
1122
|
|
1123 void
|
|
1124 macros_init(void)
|
|
1125 {
|
|
1126 macrotable_init();
|
19
|
1127 expstate_init(&mainstate, false, false);
|
17
|
1128 }
|
|
1129
|
|
1130 void
|
|
1131 macros_cleanup(void)
|
|
1132 {
|
19
|
1133 expstate_cleanup(&mainstate);
|
17
|
1134 macrotable_cleanup();
|
|
1135 }
|