327
|
1 /* Micro-C Preprocessor Part */ /* $Id$ */
|
|
2
|
|
3 #include <stdio.h>
|
|
4 #include "mc.h"
|
|
5 #include "mc-parse.h"
|
|
6 #include "mc-macro.h"
|
|
7 #include "mc-code.h"
|
|
8
|
|
9 extern struct {int fd,ln;char *name0;int inc;FILE *fcb;} *filep,filestack[FILES];
|
|
10
|
|
11 int in_macro_if = 0;
|
|
12 char *chinput;
|
|
13
|
|
14 static int mconcat=0;
|
|
15
|
|
16 static void macro_define0();
|
346
|
17 static int macro_args(char **pchptr);
|
327
|
18 static int macro_function(int macrop,char **pchptr,NMTBL *nptr,int history);
|
|
19 static void local_define(char *macro,char *value);
|
|
20 static int macro_eval(int macrop,char *body0,int history);
|
346
|
21 static char * mappend(int lists,char **result);
|
327
|
22 static int macro_processing();
|
|
23
|
|
24 static void
|
|
25 gen_source(char *s)
|
|
26 {
|
|
27 printf("%s",s);
|
|
28 }
|
|
29
|
|
30 extern void
|
|
31 macro_expansion(NMTBL *nptrm)
|
|
32 {
|
|
33 int i = mode;
|
|
34 int macrop = 0;
|
|
35 int slfree = lfree;
|
346
|
36 char *macropp;
|
|
37 struct cheap scheap;
|
327
|
38 mode = STAT;
|
|
39
|
346
|
40 save_cheap(&scheap,cheap);
|
|
41
|
327
|
42 if (nptrm->sc == FMACRO) {
|
|
43 macrop=macro_function(macrop,&chptr,nptrm,0);
|
|
44 } else {
|
|
45 macrop=macro_eval(macrop,(char *)car(nptrm->dsp),0);
|
|
46 }
|
347
|
47 cheap = reset_cheap(&scheap);
|
346
|
48 macropp = cheap->ptr;
|
347
|
49 // we can reset cheap, no page wrap in this case
|
346
|
50 mappend(reverse0(macrop),¯opp);
|
|
51 cheap->ptr[-1] ='\n';
|
|
52 cheap->ptr[0] =0;
|
327
|
53 while (mconcat) {
|
|
54 // ## re-eval macro
|
346
|
55 printf("## %s",macropp);
|
327
|
56 mconcat = 0;
|
|
57 macrop = 0;
|
346
|
58 macrop=macro_eval(macrop,macropp,0);
|
347
|
59 cheap = reset_cheap(&scheap);
|
346
|
60 macropp = cheap->ptr;
|
347
|
61 // will not override evaled list
|
346
|
62 mappend(reverse0(macrop),¯opp);
|
|
63 cheap->ptr[-1] ='\n';
|
|
64 cheap->ptr[0] =0;
|
327
|
65 }
|
346
|
66 cheap = reset_cheap(&scheap);
|
327
|
67 mconcat = 0;
|
|
68 lfree = slfree;
|
346
|
69 if (lsrc && !asmf && nptrm->sc==FMACRO) gen_comment(macropp);
|
327
|
70 chptrsave = glist2((int)chptr,chptrsave);
|
|
71 chsave = glist2(ch,chsave);
|
346
|
72 chptr = macropp;
|
327
|
73 ch = *chptr++;
|
|
74 mode = i;
|
|
75 }
|
|
76
|
|
77 /* file inclusion */
|
|
78
|
|
79 static char *
|
346
|
80 expand_file_name(char *path,char *name)
|
327
|
81 {
|
346
|
82 char *p = cheap->ptr;
|
|
83 if (! *path) return name;
|
|
84 while(( *cheap->ptr = *path++ )) cheap = increment_cheap(cheap,&p);
|
|
85 if (cheap->ptr[-1]!='/') {
|
|
86 *cheap->ptr = '/'; cheap = increment_cheap(cheap,&p);
|
|
87 }
|
|
88 while(( *cheap->ptr = *name++ )) cheap = increment_cheap(cheap,&p);
|
351
|
89 *cheap->ptr = 0;
|
|
90 cheap = increment_cheap(cheap,&p);
|
327
|
91 return p;
|
|
92 }
|
|
93
|
|
94 static FILE *
|
|
95 getfname(void)
|
|
96 {
|
|
97 int i,end='"',err=0;
|
346
|
98 char *s,*p,**pp,*name;
|
327
|
99 FILE *fp;
|
346
|
100 struct cheap scheap;
|
|
101 name = cheap->ptr;
|
327
|
102
|
|
103 getch();
|
|
104 if(skipspc()=='"') { end = '"';
|
|
105 } else if (ch=='<') { end = '>';
|
|
106 } else { error(INCERR); err=1;
|
|
107 }
|
|
108 for(i=0;(getch()!=end && ch!='\n');) {
|
346
|
109 *cheap->ptr = ch;
|
|
110 cheap = increment_cheap(cheap,&name);
|
327
|
111 }
|
|
112 if(ch=='\n') error(INCERR);
|
|
113 if (err) return filep->fcb;
|
351
|
114 *cheap->ptr = 0;
|
|
115 cheap = increment_cheap(cheap,&name);
|
|
116 save_cheap(&scheap,cheap);
|
327
|
117 fp = fopen(name,"r") ;
|
|
118 if (fp) {
|
|
119 p = name;
|
|
120 } else {
|
346
|
121 for(pp=(end=='>'||filep->inc=='>') ?l_include_path:include_path;
|
|
122 *pp;pp++) {
|
|
123 p = expand_file_name(*pp,name);
|
327
|
124 if ((fp = fopen(p,"r"))) break ;
|
|
125 }
|
|
126 }
|
|
127 if(!fp) { error(FILERR); return filep->fcb; }
|
|
128 copy_current_file_dir(s=p);
|
351
|
129 // File name determined. Dispose extra copies.
|
|
130 cheap = reset_cheap(&scheap);
|
346
|
131 if (p!=name) {
|
|
132 name = cheap->ptr;
|
|
133 while((*cheap->ptr = *s++)) cheap = increment_cheap(cheap,&name);
|
351
|
134 *cheap->ptr = 0;
|
|
135 cheap = increment_cheap(cheap,&name);
|
346
|
136 }
|
327
|
137 (filep+1)->inc = end;
|
346
|
138 (filep+1)->name0 = name;
|
327
|
139 return ( (filep+1)->fcb = fp );
|
|
140 }
|
|
141
|
|
142 /* line input and conversion */
|
|
143
|
|
144 static int macro_if_depth ;
|
|
145 static int macro_if_current ;
|
|
146 static int macro_if_skip ;
|
|
147
|
|
148 static int
|
|
149 skip_rest_of_line()
|
|
150 {
|
|
151 getch();
|
|
152 do {
|
|
153 while(ch!='\n'&&ch!='\r') {
|
|
154 if (!in_comment) {
|
|
155 if (ch=='/') {
|
|
156 getch();
|
|
157 if (ch=='/') in_comment=2;
|
|
158 else if (ch=='*') {
|
|
159 in_comment=1;
|
|
160 } else continue;
|
|
161 }
|
|
162 } else if (ch=='*') {
|
|
163 getch();
|
|
164 if (ch=='/') {
|
|
165 in_comment=0;
|
|
166 return macro_if_skip?0:1;
|
|
167 }
|
|
168 else continue;
|
|
169 }
|
|
170 getch();
|
|
171 }
|
|
172 if (in_comment==1) { getline(); getch(); }
|
|
173 } while(in_comment==1);
|
|
174 in_comment=0;
|
|
175 return 0;
|
|
176 }
|
|
177
|
343
|
178 static int next_eof;
|
|
179
|
327
|
180 extern void
|
|
181 getline(void)
|
|
182 {
|
|
183 int i;
|
|
184 int c;
|
|
185
|
343
|
186 if (next_eof) {
|
|
187 next_eof=0;
|
|
188 error(EOFERR);
|
|
189 }
|
327
|
190 do {
|
|
191 if (chinput) {
|
|
192 if (! *chinput) {
|
|
193 chinput=0;
|
|
194 continue;
|
|
195 }
|
|
196 chptr=linebuf;
|
|
197 i=0;
|
|
198 while((*chptr++=c=*chinput++)&&(c!='\n')) {
|
|
199 if (++i > LBUFSIZE-2) error(LNERR);
|
|
200 }
|
|
201 } else {
|
|
202 lineno++;
|
|
203 glineno++;
|
|
204 chptr=linebuf;
|
|
205 i=0;
|
|
206 while ((*chptr++ = c = getc(filep->fcb)) != '\n') {
|
|
207 if (++i > LBUFSIZE-2) error(LNERR);
|
|
208 if (c==EOF) {
|
343
|
209 next_eof=1;
|
|
210 --chptr;
|
|
211 break;
|
327
|
212 }
|
|
213 }
|
|
214 }
|
|
215 *chptr = '\0';
|
343
|
216 if (lsrc && !asmf && !macro_if_skip && linebuf[0]) gen_comment(linebuf);
|
327
|
217 if (*(chptr = linebuf) == '#' && !in_comment && !in_quote) {
|
|
218 if (macro_processing()) return;
|
|
219 }
|
|
220 } while(!in_quote && (macro_if_skip || linebuf[0] == '#'));
|
|
221 }
|
|
222
|
|
223 /* preprocessor directive */
|
|
224
|
|
225 /* line continuation \\ */
|
|
226
|
|
227 extern void
|
|
228 check_macro_eof()
|
|
229 {
|
|
230 int c;
|
346
|
231 // can't be in macro expansion
|
327
|
232 for(c=0;c<LBUFSIZE-3&&chptr[c];c++);
|
|
233 if (c>0&&chptr[c-1]=='\\') {
|
|
234 return;
|
|
235 } else if (c>0&&chptr[c-1]=='\n') {
|
|
236 if (c>0&&chptr[c-2]=='\\') {
|
|
237 return;
|
|
238 } else {
|
|
239 c--;
|
|
240 }
|
|
241 }
|
|
242 chptr[c] = ';';
|
|
243 chptr[c+1] = '\n';
|
|
244 chptr[c+2] = 0;
|
|
245 }
|
|
246
|
|
247 static void
|
|
248 macro_if()
|
|
249 {
|
|
250 int i;
|
|
251 ch= *chptr;
|
|
252 in_macro_if = 1;
|
|
253 check_macro_eof();
|
|
254 getsym(0);
|
|
255 /* i=cexpr(expr(1)); #if allow undefined symbols.. */
|
|
256 i=expr(1);
|
|
257 in_macro_if = 0;
|
|
258 if (car(i)==CONST) i=cadr(i);
|
|
259 else i=0;
|
|
260 if (ch) {
|
|
261 if (chptr[-1]==ch) {
|
|
262 /* we are fall into getch(), which lost the last ch */
|
|
263 /* chptr[-1]==ch check is fanatic, but ... */
|
|
264 chptr--;
|
|
265 } else error(-1);
|
|
266 }
|
|
267 macro_if_depth = macro_if_current;
|
|
268 macro_if_skip = !i;
|
|
269 }
|
|
270
|
|
271 static int
|
|
272 macro_processing()
|
|
273 {
|
|
274 int i;
|
|
275 int c;
|
|
276 int mode_save;
|
|
277
|
|
278 ++chptr;
|
|
279 while (*chptr==' '||*chptr=='\t') ++chptr;
|
361
|
280 switch(chptr[0]*chptr[1]) {
|
|
281 case 'i'*'f':
|
|
282 if ((macroeq("ifdef") || macroeq("ifndef"))) {
|
|
283 c = (chptr[-4]=='n');
|
|
284 macro_if_current++;
|
|
285 if (!macro_if_skip) {
|
|
286 mode_save = mode; mode = IFDEF;
|
|
287 ch= *chptr;
|
|
288 i = getsym(0);
|
|
289 mode = mode_save;
|
|
290 macro_if_depth = macro_if_current;
|
|
291 macro_if_skip = (!i)^c;
|
|
292 }
|
327
|
293 return 0;
|
361
|
294 } else if (macroeq("if")) {
|
|
295 macro_if_current++;
|
|
296 if (!macro_if_skip) {
|
|
297 macro_if();
|
327
|
298 }
|
|
299 return 0;
|
|
300 }
|
361
|
301 break;
|
|
302 case 'e'*'l':
|
|
303 if (macroeq("elif")) {
|
|
304 if (macro_if_current==0) {
|
|
305 error(MCERR); /* extra #else */
|
|
306 return 0;
|
|
307 }
|
|
308 if (macro_if_current == macro_if_depth) {
|
|
309 if (!macro_if_skip || macro_if_skip==2) {
|
|
310 macro_if_skip=2;
|
|
311 return 0;
|
|
312 }
|
|
313 macro_if();
|
|
314 }
|
|
315 return 0;
|
|
316 } else if (macroeq("else")) {
|
|
317 if (macro_if_current==0) {
|
|
318 error(MCERR); /* extra #else */
|
327
|
319 return 0;
|
|
320 }
|
361
|
321 if (macro_if_current == macro_if_depth) {
|
|
322 if (macro_if_skip==2) ;
|
|
323 else if (macro_if_skip) macro_if_skip=0;
|
|
324 else macro_if_skip=1;
|
|
325 }
|
|
326 return skip_rest_of_line();
|
327
|
327 }
|
361
|
328 break;
|
|
329 case 'e'*'n':
|
|
330 if (macroeq("endif")) {
|
|
331 if (macro_if_current == macro_if_depth) {
|
|
332 macro_if_skip = 0;
|
|
333 macro_if_depth = --macro_if_current;
|
|
334 } else {
|
|
335 if (macro_if_current<=0) {
|
|
336 error(MCERR); /* extra #if */
|
|
337 return 0;
|
|
338 }
|
|
339 macro_if_current--;
|
|
340 }
|
|
341 return skip_rest_of_line();
|
|
342 }
|
327
|
343 }
|
|
344 if (macro_if_skip) return 0;
|
361
|
345 switch(chptr[0]) {
|
|
346 case 'd':
|
|
347 if (macroeq("define")) {
|
|
348 ch= *chptr;
|
|
349 macro_define0();
|
|
350 *(chptr = linebuf) = '\0';
|
|
351 return 0;
|
|
352 }
|
|
353 break;
|
|
354 case 'u':
|
|
355 if (macroeq("undef")) {
|
|
356 i=mode;
|
|
357 mode=IFDEF;
|
|
358 ch= *chptr;
|
|
359 if (getsym(0)) {
|
|
360 if (nptr->sc == MACRO) {
|
|
361 nptr->sc = EMPTY;
|
|
362 } else if (nptr->sc == FMACRO) {
|
|
363 nptr->sc = EMPTY;
|
|
364 /* we cannot reclaim it's arg */
|
|
365 } else error(MCERR);
|
|
366 }
|
|
367 mode=i;
|
|
368 return 0;
|
327
|
369 }
|
361
|
370 break;
|
|
371 case 'i':
|
|
372 if (macroeq("include")) {
|
|
373 if(filep+1 >= filestack + FILES) error(FILERR);
|
|
374 if ( ((filep+1)->fcb=getfname()) == NULL) error(FILERR);
|
|
375 (filep+1)->ln=lineno;
|
|
376 lineno=0;
|
|
377 ++filep;
|
|
378 *(chptr = linebuf) = '\0';
|
|
379 return 0;
|
|
380 }
|
|
381 break;
|
327
|
382 #if ASM_CODE
|
361
|
383 case 'a':
|
|
384 if (c=='a'&¯oeq("asm")) {
|
|
385 if (asmf) error(MCERR);
|
|
386 asmf = 1;
|
327
|
387 getline();
|
361
|
388 while (asmf) {
|
|
389 gen_source(linebuf);
|
|
390 getline();
|
|
391 }
|
|
392 return 0;
|
327
|
393 }
|
361
|
394 break;
|
|
395 case 'e':
|
|
396 if (macroeq("endasm")) {
|
|
397 if (!asmf) error(MCERR);
|
|
398 asmf = 0;
|
|
399 return 0;
|
|
400 }
|
|
401 break;
|
327
|
402 #endif
|
361
|
403 case ' ': case '\t':
|
327
|
404 getline();
|
361
|
405 return 0;
|
|
406 }
|
|
407 error(MCERR);
|
327
|
408 return 0;
|
|
409 }
|
|
410
|
|
411 extern int
|
|
412 macroeq(char *s)
|
|
413 {
|
|
414 char *p;
|
|
415
|
|
416 for (p = chptr; *s;) if (*s++ != *p++) return 0;
|
|
417 chptr = p;
|
|
418 return 1;
|
|
419 }
|
|
420
|
|
421 /* macro interpreter */
|
|
422
|
|
423 extern void
|
|
424 macro_define(char *macro)
|
|
425 {
|
|
426 char *chptr_save;
|
|
427 int chsave;
|
|
428
|
|
429 chptr_save = chptr;
|
|
430 chsave = ch;
|
|
431 chptr = macro;
|
|
432 ch= *chptr++;
|
|
433 macro_define0();
|
|
434 chptr = chptr_save;
|
|
435 ch = chsave;
|
|
436 }
|
|
437
|
|
438 static void
|
|
439 macro_define0()
|
|
440 {
|
|
441 int i,args,c;
|
346
|
442 char **body;
|
327
|
443
|
|
444 i=mode;
|
|
445 mode=MDECL;
|
|
446 // ch= *chptr; ??
|
|
447 // fprintf(stderr,"macro def: ch %c *chptr %c\n",ch,*chptr);
|
|
448 getsym(0);
|
|
449 // fprintf(stderr,"macro def: %s =>",name);
|
|
450 if (nptr->sc != EMPTY) { /* override existing macro */
|
|
451 }
|
|
452 args = 0;
|
|
453 if (ch=='(') {
|
|
454 nptr->sc = FMACRO;
|
346
|
455 args = macro_args(&chptr);
|
327
|
456 } else {
|
|
457 nptr->sc = MACRO;
|
|
458 nptr->ty = -1;
|
|
459 }
|
|
460 // equal is allowed for -Dhoge=aho option
|
|
461 if (ch=='=') chptr++;
|
|
462 while((c=*chptr)==' '||c=='\t') chptr++;
|
346
|
463 nptr->dsp = list2((int)cheap->ptr,args); /* macro body */
|
|
464 body = (char **)&car(nptr->dsp);
|
|
465 while ((*cheap->ptr = c = *chptr++)
|
327
|
466 && c != '\n') {
|
346
|
467 cheap = increment_cheap(cheap,body);
|
327
|
468 if (c=='/'&&chptr[0]=='/') {
|
351
|
469 cheap->ptr--;
|
|
470 *cheap->ptr = '\0';
|
|
471 while(*chptr++); break;
|
327
|
472 } else if (c=='/'&&chptr[0]=='*') {
|
346
|
473 cheap->ptr--; chptr++;
|
327
|
474 while((c = *chptr++)) {
|
|
475 if (c=='*'&&chptr[0]=='/') {
|
|
476 c = *chptr++; break;
|
|
477 }
|
|
478 }
|
|
479 if (!c) break;
|
|
480 } else if (c=='\\' && (*chptr=='\n'||*chptr==0)) {
|
|
481 chptr++;
|
346
|
482 cheap->ptr--;
|
327
|
483 getline();
|
|
484 }
|
|
485 }
|
346
|
486 if (c=='\n') {
|
|
487 *cheap->ptr = '\0';
|
|
488 }
|
|
489 cheap = increment_cheap(cheap,body);
|
327
|
490 // fprintf(stderr,"%s\n",(char *)car(nptr->dsp));
|
|
491 mode=i;
|
|
492 }
|
|
493
|
|
494 // create function macro argument list
|
|
495 // return list2((char*)arg,next)
|
|
496
|
|
497 static int
|
346
|
498 macro_args(char **pchptr)
|
327
|
499 {
|
|
500 int c;
|
|
501 int in_quote = 0;
|
|
502 int in_wquote = 0;
|
|
503 int plevel = 0;
|
346
|
504 char **body;
|
327
|
505 char *chptr = *pchptr;
|
346
|
506 int args = list2((int)cheap->ptr,0);
|
|
507 body = (char **)&car(args);
|
327
|
508 for(;;) {
|
346
|
509 *cheap->ptr = c = *chptr++;
|
|
510 cheap = increment_cheap(cheap,body);
|
327
|
511 if (!c) {
|
|
512 chptr--;
|
|
513 error(MCERR);
|
|
514 *pchptr = chptr;
|
|
515 return reverse0(args);
|
|
516 }
|
|
517 if (in_quote) {
|
|
518 if (c=='\\') {
|
|
519 if (*chptr != '\n') {
|
346
|
520 *cheap->ptr = *chptr++;
|
|
521 cheap = increment_cheap(cheap,body);
|
327
|
522 } else {
|
|
523 getline();
|
|
524 }
|
|
525 } else if (c=='\'') {
|
|
526 in_quote = 0;
|
|
527 }
|
|
528 } else if (in_wquote) {
|
|
529 if (c=='\\') {
|
|
530 if (*chptr !='\n') {
|
346
|
531 *cheap->ptr = *chptr++;
|
|
532 cheap = increment_cheap(cheap,body);
|
327
|
533 } else {
|
346
|
534 *cheap->ptr = '\n';
|
327
|
535 getline();
|
|
536 }
|
|
537 } else if (c=='"') {
|
|
538 in_wquote = 0;
|
|
539 }
|
|
540 } else if (c=='"') {
|
|
541 in_wquote = 1;
|
|
542 } else if (c=='\'') {
|
|
543 in_quote = 1;
|
|
544 } if (plevel==0) {
|
|
545 if (c==',') {
|
346
|
546 cheap->ptr[-1] = 0;
|
|
547 args = list2((int)cheap->ptr,args);
|
|
548 body = (char **)&car(args);
|
327
|
549 } else if (c==')') {
|
346
|
550 cheap->ptr[-1] = 0;
|
327
|
551 break;
|
|
552 } else if (c=='(') {
|
|
553 plevel++;
|
|
554 } else if (c=='\\') {
|
|
555 if (*chptr=='\n') {
|
346
|
556 cheap->ptr--;
|
327
|
557 getline();
|
|
558 }
|
|
559 // } else if (c==' '||c=='\t') {
|
346
|
560 // cheap->ptr--;
|
327
|
561 } else if (c=='\n') {
|
346
|
562 cheap->ptr--;
|
327
|
563 getline();
|
|
564 chptr = *pchptr;
|
|
565 }
|
|
566 } else if (c==')') {
|
|
567 plevel--;
|
|
568 } else if (c=='(') {
|
|
569 plevel++;
|
|
570 } else if (c=='\n') {
|
346
|
571 cheap->ptr--;
|
327
|
572 getline();
|
|
573 chptr = *pchptr;
|
|
574 }
|
|
575 }
|
|
576 *pchptr = chptr;
|
|
577 return reverse0(args);
|
|
578 }
|
|
579
|
|
580 /* output macro expansion result into macrobuf (macropp) */
|
|
581
|
|
582 static int
|
|
583 macro_function(int macrop,char **pchptr,NMTBL *nptr,int history)
|
|
584 {
|
|
585 int args,sargs,values,evalues;
|
|
586 char *macro;
|
|
587
|
|
588 sargs = args = cadr(nptr->dsp);
|
346
|
589 values = macro_args(pchptr);
|
327
|
590 if (pchptr==&chptr) {
|
|
591 ch = *chptr++;
|
|
592 }
|
|
593 evalues = 0;
|
|
594 while(values) {
|
|
595 evalues = list2(macro_eval(0,(char *)car(values),history),evalues);
|
|
596 values = cadr(values);
|
|
597 }
|
|
598 evalues = reverse0(evalues);
|
359
|
599 enter_scope();
|
327
|
600 while(args) {
|
346
|
601 mappend(reverse0(car(evalues)),¯o);
|
|
602 local_define((char *)car(args),macro);
|
327
|
603 args = cadr(args);
|
|
604 evalues = cadr(evalues);
|
|
605 }
|
|
606 macro = (char *)car(nptr->dsp);
|
|
607 macrop = macro_eval(macrop,macro,list2((int)macro,history));
|
|
608 args = sargs;
|
359
|
609 leave_scope();
|
327
|
610 return macrop;
|
|
611 }
|
|
612
|
|
613 static void
|
|
614 local_define(char *macro,char *value)
|
|
615 {
|
359
|
616 NMTBL *nptr0,*nlist;
|
327
|
617 while(*macro==' '||*macro=='\t') macro++;
|
359
|
618 nptr0 = name_space_search(nlist=get_name(macro,0,DEF),MACRO);
|
|
619 nptr0 = make_local_scope(nlist,nptr0,MACRO);
|
|
620 nptr0->nm = value;
|
327
|
621 }
|
|
622
|
|
623 static int
|
|
624 macro_eval(int macrop,char *body0,int history)
|
|
625 {
|
350
|
626 int c,len;
|
327
|
627 int in_quote = 0;
|
|
628 int in_wquote = 0;
|
|
629 char *macro;
|
|
630 char *body = body0;
|
346
|
631 char **expand;
|
327
|
632 NMTBL *nptrm;
|
346
|
633 macrop = list2((int)cheap->ptr,macrop);
|
|
634 expand = (char **)&car(macrop);
|
327
|
635 for(; (c = *body++) ;) {
|
|
636 if (in_quote) {
|
|
637 if (c=='\\') {
|
346
|
638 *cheap->ptr = c; c = *body++;
|
|
639 cheap = increment_cheap(cheap,expand);
|
327
|
640 } else if (c=='\'') {
|
|
641 in_quote = 0;
|
|
642 }
|
|
643 } else if (in_wquote) {
|
|
644 if (c=='\\') {
|
346
|
645 *cheap->ptr = c; c = *body++;
|
|
646 cheap = increment_cheap(cheap,expand);
|
327
|
647 } else if (c=='"') {
|
|
648 in_wquote = 0;
|
|
649 }
|
|
650 } else if (c=='"') {
|
|
651 in_wquote = 1;
|
|
652 } else if (c=='\'') {
|
|
653 in_quote = 1;
|
|
654 } else if (c=='#' && *body=='#') {
|
|
655 // name concatenation. skip ## and re-eval macro line.
|
|
656 mconcat = 1; body++; continue;
|
|
657 } else if (alpha(c)) {
|
349
|
658 body--; // ungetc
|
350
|
659 nptrm = get_name(body,&len,NONDEF);
|
|
660 if (!nptrm) {
|
|
661 while((*cheap->ptr = *body++) && len--)
|
|
662 cheap = increment_cheap(cheap,expand);
|
|
663 body--;
|
|
664 continue;
|
|
665 }
|
|
666 body += len;
|
349
|
667 c = *body;
|
347
|
668 nptrm = name_space_search(nptrm,MACRO);
|
327
|
669 macro = (char *)car(nptrm->dsp);
|
347
|
670 switch(nptrm->sc) {
|
|
671 case FMACRO:
|
327
|
672 if (c==' '||c=='\t') {
|
|
673 while (c==' '||c=='\t') c=*body++;
|
|
674 body--;
|
|
675 }
|
|
676 if(c!='(') error(MCERR);
|
349
|
677 *cheap->ptr = 0;
|
346
|
678 cheap = increment_cheap(cheap,expand);
|
|
679 body++;
|
327
|
680 macrop = macro_function(macrop,&body,nptrm,
|
|
681 list2((int)macro,history));
|
346
|
682 macrop = list2((int)cheap->ptr,macrop);
|
|
683 expand = (char **)&(car(macrop));
|
347
|
684 break;
|
|
685 case MACRO:
|
|
686 if (neqname(nptrm->nm,macro)) {
|
|
687 if (macro[0]==0) continue;
|
|
688 *cheap->ptr = 0;
|
|
689 cheap = increment_cheap(cheap,expand);
|
|
690 macrop=macro_eval(macrop,macro,list2((int)macro,history));
|
|
691 macrop = list2((int)cheap->ptr,macrop);
|
|
692 expand = (char **)&(car(macrop));
|
|
693 break;
|
|
694 }
|
|
695 default:
|
346
|
696 macro = nptrm->nm;
|
347
|
697 case LMACRO:
|
349
|
698 while((*cheap->ptr = *macro++)/* && len-- */)
|
346
|
699 cheap = increment_cheap(cheap,expand);
|
327
|
700 }
|
|
701 continue;
|
|
702 }
|
346
|
703 *cheap->ptr = c;
|
|
704 cheap = increment_cheap(cheap,expand);
|
327
|
705 }
|
346
|
706 *cheap->ptr = 0;
|
|
707 cheap = increment_cheap(cheap,expand);
|
327
|
708 return macrop;
|
|
709 }
|
|
710
|
|
711
|
|
712 static char *
|
346
|
713 mappend(int lists,char **result)
|
327
|
714 {
|
|
715 char *p;
|
346
|
716 *result = cheap->ptr;
|
348
|
717 for(;lists;lists = cadr(lists)) {
|
327
|
718 p = (char *)car(lists);
|
347
|
719 for(;(*cheap->ptr=*p++);cheap = increment_cheap(cheap,0)) {
|
346
|
720 // in_quote + \n case ? should be \n.
|
350
|
721 if (p[-1]=='\n') cheap->ptr[0]=' ';
|
346
|
722 }
|
327
|
723 }
|
347
|
724 cheap = increment_cheap(cheap,0);
|
346
|
725 return *result;
|
327
|
726 }
|
|
727
|
|
728 /* end */
|