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