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 = '\0';
|
|
455 }
|
|
456 cheap = increment_cheap(cheap,body);
|
327
|
457 // fprintf(stderr,"%s\n",(char *)car(nptr->dsp));
|
|
458 mode=i;
|
|
459 }
|
|
460
|
|
461 // create function macro argument list
|
|
462 // return list2((char*)arg,next)
|
|
463
|
|
464 static int
|
346
|
465 macro_args(char **pchptr)
|
327
|
466 {
|
|
467 int c;
|
|
468 int in_quote = 0;
|
|
469 int in_wquote = 0;
|
|
470 int plevel = 0;
|
346
|
471 char **body;
|
327
|
472 char *chptr = *pchptr;
|
346
|
473 int args = list2((int)cheap->ptr,0);
|
|
474 body = (char **)&car(args);
|
327
|
475 for(;;) {
|
346
|
476 *cheap->ptr = c = *chptr++;
|
|
477 cheap = increment_cheap(cheap,body);
|
327
|
478 if (!c) {
|
|
479 chptr--;
|
|
480 error(MCERR);
|
|
481 *pchptr = chptr;
|
|
482 return reverse0(args);
|
|
483 }
|
|
484 if (in_quote) {
|
|
485 if (c=='\\') {
|
|
486 if (*chptr != '\n') {
|
346
|
487 *cheap->ptr = *chptr++;
|
|
488 cheap = increment_cheap(cheap,body);
|
327
|
489 } else {
|
|
490 getline();
|
|
491 }
|
|
492 } else if (c=='\'') {
|
|
493 in_quote = 0;
|
|
494 }
|
|
495 } else if (in_wquote) {
|
|
496 if (c=='\\') {
|
|
497 if (*chptr !='\n') {
|
346
|
498 *cheap->ptr = *chptr++;
|
|
499 cheap = increment_cheap(cheap,body);
|
327
|
500 } else {
|
346
|
501 *cheap->ptr = '\n';
|
327
|
502 getline();
|
|
503 }
|
|
504 } else if (c=='"') {
|
|
505 in_wquote = 0;
|
|
506 }
|
|
507 } else if (c=='"') {
|
|
508 in_wquote = 1;
|
|
509 } else if (c=='\'') {
|
|
510 in_quote = 1;
|
|
511 } if (plevel==0) {
|
|
512 if (c==',') {
|
346
|
513 cheap->ptr[-1] = 0;
|
|
514 args = list2((int)cheap->ptr,args);
|
|
515 body = (char **)&car(args);
|
327
|
516 } else if (c==')') {
|
346
|
517 cheap->ptr[-1] = 0;
|
327
|
518 break;
|
|
519 } else if (c=='(') {
|
|
520 plevel++;
|
|
521 } else if (c=='\\') {
|
|
522 if (*chptr=='\n') {
|
346
|
523 cheap->ptr--;
|
327
|
524 getline();
|
|
525 }
|
|
526 // } else if (c==' '||c=='\t') {
|
346
|
527 // cheap->ptr--;
|
327
|
528 } else if (c=='\n') {
|
346
|
529 cheap->ptr--;
|
327
|
530 getline();
|
|
531 chptr = *pchptr;
|
|
532 }
|
|
533 } else if (c==')') {
|
|
534 plevel--;
|
|
535 } else if (c=='(') {
|
|
536 plevel++;
|
|
537 } else if (c=='\n') {
|
346
|
538 cheap->ptr--;
|
327
|
539 getline();
|
|
540 chptr = *pchptr;
|
|
541 }
|
|
542 }
|
|
543 *pchptr = chptr;
|
|
544 return reverse0(args);
|
|
545 }
|
|
546
|
|
547 /* output macro expansion result into macrobuf (macropp) */
|
|
548
|
|
549 static int
|
|
550 macro_function(int macrop,char **pchptr,NMTBL *nptr,int history)
|
|
551 {
|
|
552 int args,sargs,values,evalues;
|
|
553 char *macro;
|
|
554
|
|
555 sargs = args = cadr(nptr->dsp);
|
346
|
556 values = macro_args(pchptr);
|
327
|
557 if (pchptr==&chptr) {
|
|
558 ch = *chptr++;
|
|
559 }
|
|
560 evalues = 0;
|
|
561 while(values) {
|
|
562 evalues = list2(macro_eval(0,(char *)car(values),history),evalues);
|
|
563 values = cadr(values);
|
|
564 }
|
|
565 evalues = reverse0(evalues);
|
|
566 while(args) {
|
346
|
567 mappend(reverse0(car(evalues)),¯o);
|
|
568 local_define((char *)car(args),macro);
|
327
|
569 /* fprintf(stderr,"%s: %s => %s\n",nptr->nm,(char *)car(args),(char *)car(msearch0((char *)car(args))->dsp)); */
|
|
570 args = cadr(args);
|
|
571 evalues = cadr(evalues);
|
|
572 }
|
|
573 macro = (char *)car(nptr->dsp);
|
|
574 macrop = macro_eval(macrop,macro,list2((int)macro,history));
|
|
575 /* fprintf(stderr,"%s: result %s => %s\n",nptr->nm,macro,(char *)car(macrop)); */
|
|
576 args = sargs;
|
|
577 while(args) {
|
|
578 local_undef((char *)car(args));
|
|
579 args = cadr(args);
|
|
580 }
|
|
581 return macrop;
|
|
582 }
|
|
583
|
|
584 static void
|
|
585 local_define(char *macro,char *value)
|
|
586 {
|
|
587 NMTBL *nptr0;
|
|
588 while(*macro==' '||*macro=='\t') macro++;
|
|
589 nptr0 = msearch0(macro);
|
347
|
590 /* save nptr's previous content in a list */
|
327
|
591 nptr0->ty=list3(nptr0->sc,nptr0->ty,nptr0->dsp);
|
|
592 /* set new value */
|
|
593 nptr0->sc=LMACRO;
|
|
594 nptr0->dsp=list2((int)value,0);
|
|
595 }
|
|
596
|
|
597 static void
|
|
598 local_undef(char *macro)
|
|
599 {
|
|
600 NMTBL *nptr0;
|
|
601 int save;
|
|
602 nptr0 = msearch0(macro);
|
|
603 save = nptr0->ty;
|
|
604 nptr0->sc=car(save);
|
|
605 nptr0->dsp=caddr(save);
|
|
606 nptr0->ty=cadr(save);
|
|
607 }
|
|
608
|
|
609 static int
|
|
610 macro_eval(int macrop,char *body0,int history)
|
|
611 {
|
350
|
612 int c,len;
|
327
|
613 int in_quote = 0;
|
|
614 int in_wquote = 0;
|
|
615 char *macro;
|
|
616 char *body = body0;
|
346
|
617 char **expand;
|
327
|
618 NMTBL *nptrm;
|
346
|
619 macrop = list2((int)cheap->ptr,macrop);
|
|
620 expand = (char **)&car(macrop);
|
327
|
621 for(; (c = *body++) ;) {
|
|
622 if (in_quote) {
|
|
623 if (c=='\\') {
|
346
|
624 *cheap->ptr = c; c = *body++;
|
|
625 cheap = increment_cheap(cheap,expand);
|
327
|
626 } else if (c=='\'') {
|
|
627 in_quote = 0;
|
|
628 }
|
|
629 } else if (in_wquote) {
|
|
630 if (c=='\\') {
|
346
|
631 *cheap->ptr = c; c = *body++;
|
|
632 cheap = increment_cheap(cheap,expand);
|
327
|
633 } else if (c=='"') {
|
|
634 in_wquote = 0;
|
|
635 }
|
|
636 } else if (c=='"') {
|
|
637 in_wquote = 1;
|
|
638 } else if (c=='\'') {
|
|
639 in_quote = 1;
|
|
640 } else if (c=='#' && *body=='#') {
|
|
641 // name concatenation. skip ## and re-eval macro line.
|
|
642 mconcat = 1; body++; continue;
|
|
643 } else if (alpha(c)) {
|
349
|
644 body--; // ungetc
|
350
|
645 nptrm = get_name(body,&len,NONDEF);
|
|
646 if (!nptrm) {
|
|
647 while((*cheap->ptr = *body++) && len--)
|
|
648 cheap = increment_cheap(cheap,expand);
|
|
649 body--;
|
|
650 continue;
|
|
651 }
|
|
652 body += len;
|
349
|
653 c = *body;
|
347
|
654 nptrm = name_space_search(nptrm,MACRO);
|
327
|
655 macro = (char *)car(nptrm->dsp);
|
347
|
656 switch(nptrm->sc) {
|
|
657 case FMACRO:
|
327
|
658 if (c==' '||c=='\t') {
|
|
659 while (c==' '||c=='\t') c=*body++;
|
|
660 body--;
|
|
661 }
|
|
662 if(c!='(') error(MCERR);
|
349
|
663 *cheap->ptr = 0;
|
346
|
664 cheap = increment_cheap(cheap,expand);
|
|
665 body++;
|
327
|
666 macrop = macro_function(macrop,&body,nptrm,
|
|
667 list2((int)macro,history));
|
346
|
668 macrop = list2((int)cheap->ptr,macrop);
|
|
669 expand = (char **)&(car(macrop));
|
347
|
670 break;
|
|
671 case MACRO:
|
|
672 if (neqname(nptrm->nm,macro)) {
|
|
673 if (macro[0]==0) continue;
|
|
674 *cheap->ptr = 0;
|
|
675 cheap = increment_cheap(cheap,expand);
|
|
676 macrop=macro_eval(macrop,macro,list2((int)macro,history));
|
|
677 macrop = list2((int)cheap->ptr,macrop);
|
|
678 expand = (char **)&(car(macrop));
|
|
679 break;
|
|
680 }
|
|
681 default:
|
346
|
682 macro = nptrm->nm;
|
347
|
683 case LMACRO:
|
349
|
684 while((*cheap->ptr = *macro++)/* && len-- */)
|
346
|
685 cheap = increment_cheap(cheap,expand);
|
327
|
686 }
|
|
687 continue;
|
|
688 }
|
346
|
689 *cheap->ptr = c;
|
|
690 cheap = increment_cheap(cheap,expand);
|
327
|
691 }
|
346
|
692 *cheap->ptr = 0;
|
|
693 cheap = increment_cheap(cheap,expand);
|
327
|
694 return macrop;
|
|
695 }
|
|
696
|
|
697
|
|
698 static char *
|
346
|
699 mappend(int lists,char **result)
|
327
|
700 {
|
|
701 char *p;
|
346
|
702 *result = cheap->ptr;
|
348
|
703 for(;lists;lists = cadr(lists)) {
|
327
|
704 p = (char *)car(lists);
|
347
|
705 for(;(*cheap->ptr=*p++);cheap = increment_cheap(cheap,0)) {
|
346
|
706 // in_quote + \n case ? should be \n.
|
350
|
707 if (p[-1]=='\n') cheap->ptr[0]=' ';
|
346
|
708 }
|
327
|
709 }
|
347
|
710 cheap = increment_cheap(cheap,0);
|
346
|
711 return *result;
|
327
|
712 }
|
|
713
|
|
714 /* end */
|