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