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