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