10 #if defined(__IBMC__) || defined(__EMX__)
11 # define PATH_MAX _MAX_PATH
20 * do a macro definition. tp points to the name being defined in the line
23 dodefine(Tokenrow
* trp
)
29 static uchar location
[(PATH_MAX
+ 8) * NINC
], *cp
;
32 if (tp
>= trp
->lp
|| tp
->type
!= NAME
)
34 error(ERROR
, "#defined token is not a name");
38 if (np
->flag
& ISUNCHANGE
)
40 error(ERROR
, "#defined token %t can't be redefined", tp
);
43 /* collect arguments */
46 if (tp
< trp
->lp
&& tp
->type
== LP
&& tp
->wslen
== 0)
53 maketokenrow(2, args
);
67 if (narg
>= args
->max
)
69 for (atp
= args
->bp
; atp
< args
->lp
; atp
++)
70 if (atp
->len
== tp
->len
71 && strncmp((char *) atp
->t
, (char *) tp
->t
, tp
->len
) == 0)
72 error(ERROR
, "Duplicate macro argument");
78 if (tp
->type
!= COMMA
)
87 error(ERROR
, "Syntax error in macro parameters");
94 if (((trp
->lp
) - 1)->type
== NL
)
96 def
= normtokenrow(trp
);
97 if (np
->flag
& ISDEFINED
)
99 if (comparetokens(def
, np
->vp
)
100 || (np
->ap
== NULL
) != (args
== NULL
)
101 || (np
->ap
&& comparetokens(args
, np
->ap
)))
105 "Macro redefinition of %t (already defined at %s)",
106 trp
->bp
+ 2, np
->loc
);
109 "Macro redefinition of %t (already defined at %s)",
110 trp
->bp
+ 2, "commandline" );
117 tap
= normtokenrow(args
);
123 np
->flag
|= ISDEFINED
;
125 /* build location string of macro definition */
126 for (cp
= location
, s
= cursource
; s
; s
= s
->next
)
131 sprintf((char *)cp
, "%s:%d", s
->filename
, s
->line
);
132 cp
+= strlen((char *)cp
);
135 np
->loc
= newstring(location
, strlen((char *)location
), 0);
140 error(INFO
, "Macro definition of %s(%r) [%r]", np
->name
, np
->ap
, np
->vp
);
142 error(INFO
, "Macro definition of %s [%r]", np
->name
, np
->vp
);
147 * Definition received via -D or -U
150 doadefine(Tokenrow
* trp
, int type
)
153 static uchar onestr
[2] = "1";
154 static Token onetoken
[1] = {{NUMBER
, 0, 0, 1, onestr
, 0}};
155 static Tokenrow onetr
= {onetoken
, onetoken
, onetoken
+ 1, 1};
160 if (trp
->lp
- trp
->tp
!= 2 || trp
->tp
->type
!= NAME
)
162 if ((np
= lookup(trp
->tp
, 0)) == NULL
)
164 np
->flag
&= ~ISDEFINED
;
170 if (trp
->tp
>= trp
->lp
|| trp
->tp
->type
!= NAME
)
172 trp
->tp
->type
= ARCHITECTURE
;
173 np
= lookup(trp
->tp
, 1);
174 np
->flag
|= ISARCHITECTURE
;
176 if (trp
->tp
>= trp
->lp
|| trp
->tp
->type
== END
)
182 error(FATAL
, "Illegal -A argument %r", trp
);
185 if (trp
->tp
>= trp
->lp
|| trp
->tp
->type
!= NAME
)
187 np
= lookup(trp
->tp
, 1);
188 np
->flag
|= ISDEFINED
;
190 if (trp
->tp
>= trp
->lp
|| trp
->tp
->type
== END
)
195 if (trp
->tp
->type
!= ASGN
)
198 if ((trp
->lp
- 1)->type
== END
)
200 np
->vp
= normtokenrow(trp
);
203 error(FATAL
, "Illegal -D or -U argument %r", trp
);
209 * Do macro expansion in a row of tokens.
210 * Flag is NULL if more input can be gathered.
213 expandrow(Tokenrow
* trp
, char *flag
)
218 MacroValidatorList validators
;
219 mvl_init(&validators
);
220 /* Sets all token-identifiers to 0 because tokens may not be initialised (never use C!) */
221 tokenrow_zeroTokenIdentifiers(trp
);
224 setsource(flag
, -1, -1, "", 0);
225 for (tp
= trp
->tp
; tp
< trp
->lp
;)
227 mvl_check(&validators
, tp
);
230 || quicklook(tp
->t
[0], tp
->len
> 1 ? tp
->t
[1] : 0) == 0
231 || (np
= lookup(tp
, 0)) == NULL
232 || (np
->flag
& (ISDEFINED
| ISMAC
)) == 0
233 || (np
->flag
& ISACTIVE
) != 0)
239 if (np
->val
== KDEFINED
)
242 if ((tp
+ 1) < trp
->lp
&& (tp
+ 1)->type
== NAME
)
243 (tp
+ 1)->type
= NAME1
;
245 if ((tp
+ 3) < trp
->lp
&& (tp
+ 1)->type
== LP
246 && (tp
+ 2)->type
== NAME
&& (tp
+ 3)->type
== RP
)
247 (tp
+ 2)->type
= NAME1
;
249 error(ERROR
, "Incorrect syntax for `defined'");
254 if (np
->val
== KMACHINE
)
256 if (((tp
- 1) >= trp
->bp
) && ((tp
- 1)->type
== SHARP
))
258 tp
->type
= ARCHITECTURE
;
259 if ((tp
+ 1) < trp
->lp
&& (tp
+ 1)->type
== NAME
)
260 (tp
+ 1)->type
= NAME2
;
262 if ((tp
+ 3) < trp
->lp
&& (tp
+ 1)->type
== LP
263 && (tp
+ 2)->type
== NAME
&& (tp
+ 3)->type
== RP
)
264 (tp
+ 2)->type
= NAME2
;
266 error(ERROR
, "Incorrect syntax for `#machine'");
272 if (np
->flag
& ISMAC
)
273 builtin(trp
, np
->val
);
275 expand(trp
, np
, &validators
);
281 mvl_destruct(&validators
);
285 * Expand the macro whose name is np, at token trp->tp, in the tokenrow.
286 * Return trp->tp at the first token next to be expanded
287 * (ordinarily the beginning of the expansion)
288 * I.e.: the same position as before!
289 * Only one expansion is performed, then we return to the expandrow()
290 * loop and start at same position.
293 expand(Tokenrow
* trp
, Nlist
* np
, MacroValidatorList
* pValidators
)
295 // Token * pOldNextTp;
298 Tokenrow
*atr
[NARG
+ 1];
303 error(INFO
, "Macro expansion of %t with %s(%r)", trp
->tp
, np
->name
, np
->ap
);
305 error(INFO
, "Macro expansion of %t with %s", trp
->tp
, np
->name
);
308 copytokenrow(&ntr
, np
->vp
); /* copy macro value */
309 if (np
->ap
== NULL
) /* parameterless */
313 ntokc
= gatherargs(trp
, atr
, &narg
);
315 { /* not actually a call (no '(') */
319 if (narg
!= rowlen(np
->ap
))
321 error(ERROR
, "Disagreement in number of macro arguments");
326 /** If gatherargs passed a macro validating token, this token
327 must become valid here.
328 trp->tp+0 was checked in expandrow(), so we dont need to do it
331 for (i
= 1; i
< ntokc
; i
++)
333 mvl_check(pValidators
,trp
->tp
+i
);
336 substargs(np
, &ntr
, atr
); /* put args into replacement */
337 for (i
= 0; i
< narg
; i
++)
345 np->flag |= ISACTIVE;
350 doconcat(&ntr
); /* execute ## operators */
352 makespace(&ntr
, trp
->tp
);
355 // expandrow(&ntr, "<expand>");
356 // insertrow(trp, ntokc, &ntr);
358 // np->flag &= ~ISACTIVE;
362 // Replace macro by its value:
364 // pOldNextTp = trp->tp+ntokc;
365 tokenrow_zeroTokenIdentifiers(&ntr
);
366 insertrow(trp
, ntokc
, &ntr
);
367 /* Reassign old macro validators:
369 // mvl_move(pValidators, trp->tp - pOldNextTp);
371 /* add validator for just invalidated macro:
373 np
->flag
|= ISACTIVE
;
374 if (trp
->tp
!= trp
->lp
)
375 { /* tp is a valid pointer: */
376 mvl_add(pValidators
,np
,trp
->tp
);
379 { /* tp is == lp, therefore does not point to valid memory: */
380 mvl_add(pValidators
,np
,0);
382 /* reset trp->tp to original position:
384 trp
->tp
-= ntr
.lp
- ntr
.bp
; /* so the result will be tested for macros from the same position again */
392 * Gather an arglist, starting in trp with tp pointing at the macro name.
393 * Return total number of tokens passed, stash number of args found.
394 * trp->tp is not changed relative to the tokenrow.
397 gatherargs(Tokenrow
* trp
, Tokenrow
** atr
, int *narg
)
406 *narg
= -1; /* means that there is no macro
413 if (trp
->tp
>= trp
->lp
)
416 if ((trp
->lp
- 1)->type
== END
)
423 if (trp
->tp
->type
== LP
)
425 if (trp
->tp
->type
!= NL
)
432 /* search for the terminating ), possibly extending the row */
436 if (trp
->tp
>= trp
->lp
)
441 /* makespace(trp); [rh] */
443 if (trp
->tp
->type
== END
)
447 error(ERROR
, "EOF in macro arglist");
450 if (trp
->tp
->type
== NL
)
455 /* makespace(trp); [rh] */
459 if (trp
->tp
->type
== LP
)
462 if (trp
->tp
->type
== RP
)
468 /* Now trp->tp won't move underneath us */
469 lp
= bp
= trp
->tp
+ ntokp
;
470 for (; parens
>= 0; lp
++)
479 if (lp
->type
== DSHARP
)
480 lp
->type
= DSHARP1
; /* ## not special in arg */
481 if ((lp
->type
== COMMA
&& parens
== 0) ||
482 ( parens
< 0 && ((lp
- 1)->type
!= LP
)))
484 if (*narg
>= NARG
- 1)
485 error(FATAL
, "Sorry, too many macro arguments");
486 ttr
.bp
= ttr
.tp
= bp
;
488 atr
[(*narg
)++] = normtokenrow(&ttr
);
496 * substitute the argument list into the replacement string
497 * This would be simple except for ## and #
500 substargs(Nlist
* np
, Tokenrow
* rtr
, Tokenrow
** atr
)
506 for (rtr
->tp
= rtr
->bp
; rtr
->tp
< rtr
->lp
;)
508 if (rtr
->tp
->type
== SHARP
)
509 { /* string operator */
512 if ((argno
= lookuparg(np
, rtr
->tp
)) < 0)
514 error(ERROR
, "# not followed by macro parameter");
517 ntok
= 1 + (rtr
->tp
- tp
);
519 insertrow(rtr
, ntok
, stringify(atr
[argno
]));
522 if (rtr
->tp
->type
== NAME
523 && (argno
= lookuparg(np
, rtr
->tp
)) >= 0)
525 if (((rtr
->tp
+ 1) < rtr
->lp
&& (rtr
->tp
+ 1)->type
== DSHARP
)
526 || (rtr
->tp
!= rtr
->bp
&& (rtr
->tp
- 1)->type
== DSHARP
))
528 copytokenrow(&tatr
, atr
[argno
]);
529 makespace(&tatr
, rtr
->tp
);
530 insertrow(rtr
, 1, &tatr
);
535 copytokenrow(&tatr
, atr
[argno
]);
536 makespace(&tatr
, rtr
->tp
);
537 expandrow(&tatr
, "<macro>");
538 insertrow(rtr
, 1, &tatr
);
548 * Evaluate the ## operators in a tokenrow
551 doconcat(Tokenrow
* trp
)
557 for (trp
->tp
= trp
->bp
; trp
->tp
< trp
->lp
; trp
->tp
++)
559 if (trp
->tp
->type
== DSHARP1
)
560 trp
->tp
->type
= DSHARP
;
562 if (trp
->tp
->type
== DSHARP
)
570 if (ltp
< trp
->bp
|| ntp
>= trp
->lp
)
572 error(ERROR
, "## occurs at border of replacement");
582 if (len
+ ntp
->len
+ ntp
->wslen
> sizeof(tt
))
584 error(ERROR
, "## string concatination buffer overrun");
588 if (ntp
!= trp
->tp
+ 1)
590 strncpy((char *) tt
+ len
, (char *) ntp
->t
- ntp
->wslen
,
591 ntp
->len
+ ntp
->wslen
);
592 len
+= ntp
->len
+ ntp
->wslen
;
594 else // Leerzeichen um ## herum entfernen:
596 strncpy((char *) tt
+ len
, (char *) ntp
->t
, ntp
->len
);
603 while (ntp
< trp
->lp
);
606 setsource("<##>", -1, -1, tt
, 0);
607 maketokenrow(3, &ntr
);
610 if (ntr
.bp
->type
== UNCLASS
)
611 error(WARNING
, "Bad token %r produced by ##", &ntr
);
612 while ((ntr
.lp
-1)->len
== 0 && ntr
.lp
!= ntr
.bp
)
617 makespace(&ntr
, ltp
);
618 insertrow(trp
, ntp
- ltp
, &ntr
);
626 * tp is a potential parameter name of macro mac;
627 * look it up in mac's arglist, and if found, return the
628 * corresponding index in the argname array. Return -1 if not found.
631 lookuparg(Nlist
* mac
, Token
* tp
)
635 if (tp
->type
!= NAME
|| mac
->ap
== NULL
)
637 for (ap
= mac
->ap
->bp
; ap
< mac
->ap
->lp
; ap
++)
639 if (ap
->len
== tp
->len
&& strncmp((char *) ap
->t
, (char *) tp
->t
, ap
->len
) == 0)
640 return ap
- mac
->ap
->bp
;
646 * Return a quoted version of the tokenrow (from # arg)
650 stringify(Tokenrow
* vp
)
652 static Token t
= {STRING
, 0, 0, 0, NULL
, 0};
653 static Tokenrow tr
= {&t
, &t
, &t
+ 1, 1};
660 for (tp
= vp
->bp
; tp
< vp
->lp
; tp
++)
662 instring
= tp
->type
== STRING
|| tp
->type
== CCON
;
663 if (sp
+ 2 * tp
->len
+ tp
->wslen
>= &s
[STRLEN
- 10])
665 error(ERROR
, "Stringified macro arg is too long");
669 // Change by np 31.10.2001, #93725 - begin
674 for (i
= 0, cp
= tp
->t
; (unsigned int)i
< tp
->len
; i
++)
676 if (instring
&& (*cp
== '"' || *cp
== '\\'))
684 t
.len
= strlen((char *) sp
);
685 t
.t
= newstring(sp
, t
.len
, 0);
690 * expand a builtin name
693 builtin(Tokenrow
* trp
, int biname
)
701 /* need to find the real source */
703 while (s
&& s
->fd
== -1)
707 /* most are strings */
721 op
= outnum(op
- 1, s
->line
);
726 char *src
= s
->filename
;
728 while ((*op
++ = *src
++) != 0)
736 strncpy(op
, curtime
+ 4, 7);
737 strncpy(op
+ 7, curtime
+ 20, 4);
742 strncpy(op
, curtime
+ 11, 8);
747 error(ERROR
, "cpp botch: unknown internal macro");
750 if (tp
->type
== STRING
)
752 tp
->t
= (uchar
*) outptr
;
753 tp
->len
= op
- outptr
;