7
* do a macro definition. tp points to the name being defined in the line
10
dodefine(Tokenrow *trp)
17
if (tp>=trp->lp || tp->type!=NAME) {
18
error(ERROR, "#defined token is not a name");
22
if (np->flag&ISUNCHANGE) {
23
error(ERROR, "#defined token %t can't be redefined", tp);
26
/* collect arguments */
29
if (tp<trp->lp && tp->type==LP && tp->wslen==0) {
34
maketokenrow(2, args);
45
for (atp=args->bp; atp<args->lp; atp++)
47
&& strncmp((char*)atp->t, (char*)tp->t, tp->len)==0)
48
error(ERROR, "Duplicate macro argument");
54
if (tp->type!=COMMA) {
61
error(ERROR, "Syntax error in macro parameters");
68
if (((trp->lp)-1)->type==NL)
70
def = normtokenrow(trp);
71
if (np->flag&ISDEFINED) {
72
if (comparetokens(def, np->vp)
73
|| (np->ap==NULL) != (args==NULL)
74
|| (np->ap && comparetokens(args, np->ap)))
75
error(ERROR, "Macro redefinition of %t", trp->bp+2);
79
tap = normtokenrow(args);
85
np->flag |= ISDEFINED;
89
* Definition received via -D or -U
92
doadefine(Tokenrow *trp, int type)
95
static Token onetoken[1] = {{ NUMBER, 0, 0, 0, 1, (uchar*)"1" }};
96
static Tokenrow onetr = { onetoken, onetoken, onetoken+1, 1 };
100
if (trp->lp-trp->tp != 2 || trp->tp->type!=NAME)
102
if ((np = lookup(trp->tp, 0)) == NULL)
104
np->flag &= ~ISDEFINED;
107
if (trp->tp >= trp->lp || trp->tp->type!=NAME)
109
np = lookup(trp->tp, 1);
110
np->flag |= ISDEFINED;
112
if (trp->tp >= trp->lp || trp->tp->type==END) {
116
if (trp->tp->type!=ASGN)
119
if ((trp->lp-1)->type == END)
121
np->vp = normtokenrow(trp);
124
error(FATAL, "Illegal -D or -U argument %r", trp);
128
* Do macro expansion in a row of tokens.
129
* Flag is NULL if more input can be gathered.
132
expandrow(Tokenrow *trp, char *flag)
138
setsource(flag, -1, "");
139
for (tp = trp->tp; tp<trp->lp; ) {
141
|| quicklook(tp->t[0], tp->len>1?tp->t[1]:0)==0
142
|| (np = lookup(tp, 0))==NULL
143
|| (np->flag&(ISDEFINED|ISMAC))==0
144
|| (tp->hideset && checkhideset(tp->hideset, np))) {
149
if (np->val==KDEFINED) {
151
if ((tp+1)<trp->lp && (tp+1)->type==NAME)
152
(tp+1)->type = NAME1;
153
else if ((tp+3)<trp->lp && (tp+1)->type==LP
154
&& (tp+2)->type==NAME && (tp+3)->type==RP)
155
(tp+2)->type = NAME1;
157
error(ERROR, "Incorrect syntax for `defined'");
162
builtin(trp, np->val);
173
* Expand the macro whose name is np, at token trp->tp, in the tokenrow.
174
* Return trp->tp at the first token next to be expanded
175
* (ordinarily the beginning of the expansion)
178
expand(Tokenrow *trp, Nlist *np)
183
Tokenrow *atr[NARG+1];
186
copytokenrow(&ntr, np->vp); /* copy macro value */
187
if (np->ap==NULL) /* parameterless */
190
ntokc = gatherargs(trp, atr, &narg);
191
if (narg<0) { /* not actually a call (no '(') */
195
if (narg != rowlen(np->ap)) {
196
error(ERROR, "Disagreement in number of macro arguments");
197
trp->tp->hideset = newhideset(trp->tp->hideset, np);
201
substargs(np, &ntr, atr); /* put args into replacement */
202
for (i=0; i<narg; i++) {
207
doconcat(&ntr); /* execute ## operators */
208
hs = newhideset(trp->tp->hideset, np);
209
for (tp=ntr.bp; tp<ntr.lp; tp++) { /* distribute hidesets */
210
if (tp->type==NAME) {
214
tp->hideset = unionhideset(tp->hideset, hs);
218
insertrow(trp, ntokc, &ntr);
219
trp->tp -= rowlen(&ntr);
225
* Gather an arglist, starting in trp with tp pointing at the macro name.
226
* Return total number of tokens passed, stash number of args found.
227
* trp->tp is not changed relative to the tokenrow.
230
gatherargs(Tokenrow *trp, Tokenrow **atr, int *narg)
239
*narg = -1; /* means that there is no macro call */
244
if (trp->tp >= trp->lp) {
246
if ((trp->lp-1)->type==END) {
252
if (trp->tp->type==LP)
254
if (trp->tp->type!=NL)
261
/* search for the terminating ), possibly extending the row */
264
if (trp->tp >= trp->lp)
270
if (trp->tp->type==END) {
273
error(ERROR, "EOF in macro arglist");
276
if (trp->tp->type==NL) {
284
if (trp->tp->type==LP)
286
else if (trp->tp->type==RP)
292
/* Now trp->tp won't move underneath us */
293
lp = bp = trp->tp+ntokp;
294
for (; parens>=0; lp++) {
295
if (lp->type == LP) {
301
if (lp->type==DSHARP)
302
lp->type = DSHARP1; /* ## not special in arg */
303
if ((lp->type==COMMA && parens==0) || (parens<0 && (lp-1)->type!=LP)) {
305
error(FATAL, "Sorry, too many macro arguments");
306
ttr.bp = ttr.tp = bp;
308
atr[(*narg)++] = normtokenrow(&ttr);
316
* substitute the argument list into the replacement string
317
* This would be simple except for ## and #
320
substargs(Nlist *np, Tokenrow *rtr, Tokenrow **atr)
326
for (rtr->tp=rtr->bp; rtr->tp<rtr->lp; ) {
327
if (rtr->tp->type==SHARP) { /* string operator */
330
if ((argno = lookuparg(np, rtr->tp))<0) {
331
error(ERROR, "# not followed by macro parameter");
334
ntok = 1 + (rtr->tp - tp);
336
insertrow(rtr, ntok, stringify(atr[argno]));
339
if (rtr->tp->type==NAME
340
&& (argno = lookuparg(np, rtr->tp)) >= 0) {
341
if ((rtr->tp+1)->type==DSHARP
342
|| (rtr->tp!=rtr->bp && (rtr->tp-1)->type==DSHARP))
343
insertrow(rtr, 1, atr[argno]);
345
copytokenrow(&tatr, atr[argno]);
346
expandrow(&tatr, "<macro>");
347
insertrow(rtr, 1, &tatr);
357
* Evaluate the ## operators in a tokenrow
360
doconcat(Tokenrow *trp)
366
for (trp->tp=trp->bp; trp->tp<trp->lp; trp->tp++) {
367
if (trp->tp->type==DSHARP1)
368
trp->tp->type = DSHARP;
369
else if (trp->tp->type==DSHARP) {
373
if (ltp<trp->bp || ntp>=trp->lp) {
374
error(ERROR, "## occurs at border of replacement");
377
len = ltp->len + ntp->len;
378
strncpy((char*)tt, (char*)ltp->t, ltp->len);
379
strncpy((char*)tt+ltp->len, (char*)ntp->t, ntp->len);
381
setsource("<##>", -1, tt);
382
maketokenrow(3, &ntr);
385
if (ntr.lp-ntr.bp!=2 || ntr.bp->type==UNCLASS)
386
error(WARNING, "Bad token %r produced by ##", &ntr);
390
insertrow(trp, (ntp-ltp)+1, &ntr);
398
* tp is a potential parameter name of macro mac;
399
* look it up in mac's arglist, and if found, return the
400
* corresponding index in the argname array. Return -1 if not found.
403
lookuparg(Nlist *mac, Token *tp)
407
if (tp->type!=NAME || mac->ap==NULL)
409
for (ap=mac->ap->bp; ap<mac->ap->lp; ap++) {
410
if (ap->len==tp->len && strncmp((char*)ap->t,(char*)tp->t,ap->len)==0)
411
return ap - mac->ap->bp;
417
* Return a quoted version of the tokenrow (from # arg)
421
stringify(Tokenrow *vp)
423
static Token t = { STRING };
424
static Tokenrow tr = { &t, &t, &t+1, 1 };
431
for (tp = vp->bp; tp < vp->lp; tp++) {
432
instring = tp->type==STRING || tp->type==CCON;
433
if (sp+2*tp->len >= &s[STRLEN-10]) {
434
error(ERROR, "Stringified macro arg is too long");
437
if (tp->wslen && (tp->flag&XPWS)==0)
439
for (i=0, cp=tp->t; i<tp->len; i++) {
440
if (instring && (*cp=='"' || *cp=='\\'))
448
t.len = strlen((char*)sp);
449
t.t = newstring(sp, t.len, 0);
454
* expand a builtin name
457
builtin(Tokenrow *trp, int biname)
465
/* need to find the real source */
467
while (s && s->fd==-1)
471
/* most are strings */
483
op = outnum(op-1, s->line);
487
char *src = s->filename;
488
while ((*op++ = *src++) != 0)
496
strncpy(op, curtime+4, 7);
497
strncpy(op+7, curtime+20, 4);
502
strncpy(op, curtime+11, 8);
507
error(ERROR, "cpp botch: unknown internal macro");
510
if (tp->type==STRING)
512
tp->t = (uchar*)outp;