1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "cpp.h"
5 
6 /*
7  * do a macro definition.  tp points to the name being defined in the line
8  */
9 void
dodefine(Tokenrow * trp)10 dodefine(Tokenrow *trp)
11 {
12 	Token *tp;
13 	Nlist *np;
14 	Tokenrow *def, *args;
15 
16 	tp = trp->tp+1;
17 	if (tp>=trp->lp || tp->type!=NAME) {
18 		error(ERROR, "#defined token is not a name");
19 		return;
20 	}
21 	np = lookup(tp, 1);
22 	if (np->flag&ISUNCHANGE) {
23 		error(ERROR, "#defined token %t can't be redefined", tp);
24 		return;
25 	}
26 	/* collect arguments */
27 	tp += 1;
28 	args = NULL;
29 	if (tp<trp->lp && tp->type==LP && tp->wslen==0) {
30 		/* macro with args */
31 		int narg = 0;
32 		tp += 1;
33 		args = new(Tokenrow);
34 		maketokenrow(2, args);
35 		if (tp->type!=RP) {
36 			int err = 0;
37 			for (;;) {
38 				Token *atp;
39 				if (tp->type!=NAME) {
40 					err++;
41 					break;
42 				}
43 				if (narg>=args->max)
44 					growtokenrow(args);
45 				for (atp=args->bp; atp<args->lp; atp++)
46 					if (atp->len==tp->len
47 					 && strncmp((char*)atp->t, (char*)tp->t, tp->len)==0)
48 						error(ERROR, "Duplicate macro argument");
49 				*args->lp++ = *tp;
50 				narg++;
51 				tp += 1;
52 				if (tp->type==RP)
53 					break;
54 				if (tp->type!=COMMA) {
55 					err++;
56 					break;
57 				}
58 				tp += 1;
59 			}
60 			if (err) {
61 				error(ERROR, "Syntax error in macro parameters");
62 				return;
63 			}
64 		}
65 		tp += 1;
66 	}
67 	trp->tp = tp;
68 	if (((trp->lp)-1)->type==NL)
69 		trp->lp -= 1;
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);
76 	}
77 	if (args) {
78 		Tokenrow *tap;
79 		tap = normtokenrow(args);
80 		dofree(args->bp);
81 		args = tap;
82 	}
83 	np->ap = args;
84 	np->vp = def;
85 	np->flag |= ISDEFINED;
86 }
87 
88 /*
89  * Definition received via -D or -U
90  */
91 void
doadefine(Tokenrow * trp,int type)92 doadefine(Tokenrow *trp, int type)
93 {
94 	Nlist *np;
95 	static Token onetoken[1] = {{ NUMBER, 0, 0, 0, 1, (uchar*)"1" }};
96 	static Tokenrow onetr = { onetoken, onetoken, onetoken+1, 1 };
97 
98 	trp->tp = trp->bp;
99 	if (type=='U') {
100 		if (trp->lp-trp->tp != 2 || trp->tp->type!=NAME)
101 			goto syntax;
102 		if ((np = lookup(trp->tp, 0)) == NULL)
103 			return;
104 		np->flag &= ~ISDEFINED;
105 		return;
106 	}
107 	if (trp->tp >= trp->lp || trp->tp->type!=NAME)
108 		goto syntax;
109 	np = lookup(trp->tp, 1);
110 	np->flag |= ISDEFINED;
111 	trp->tp += 1;
112 	if (trp->tp >= trp->lp || trp->tp->type==END) {
113 		np->vp = &onetr;
114 		return;
115 	}
116 	if (trp->tp->type!=ASGN)
117 		goto syntax;
118 	trp->tp += 1;
119 	if ((trp->lp-1)->type == END)
120 		trp->lp -= 1;
121 	np->vp = normtokenrow(trp);
122 	return;
123 syntax:
124 	error(FATAL, "Illegal -D or -U argument %r", trp);
125 }
126 
127 /*
128  * Do macro expansion in a row of tokens.
129  * Flag is NULL if more input can be gathered.
130  */
131 void
expandrow(Tokenrow * trp,char * flag)132 expandrow(Tokenrow *trp, char *flag)
133 {
134 	Token *tp;
135 	Nlist *np;
136 
137 	if (flag)
138 		setsource(flag, -1, "");
139 	for (tp = trp->tp; tp<trp->lp; ) {
140 		if (tp->type!=NAME
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))) {
145 			tp++;
146 			continue;
147 		}
148 		trp->tp = tp;
149 		if (np->val==KDEFINED) {
150 			tp->type = DEFINED;
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;
156 			else
157 				error(ERROR, "Incorrect syntax for `defined'");
158 			tp++;
159 			continue;
160 		}
161 		if (np->flag&ISMAC)
162 			builtin(trp, np->val);
163 		else {
164 			expand(trp, np);
165 		}
166 		tp = trp->tp;
167 	}
168 	if (flag)
169 		unsetsource();
170 }
171 
172 /*
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)
176  */
177 void
expand(Tokenrow * trp,Nlist * np)178 expand(Tokenrow *trp, Nlist *np)
179 {
180 	Tokenrow ntr;
181 	int ntokc, narg, i;
182 	Token *tp;
183 	Tokenrow *atr[NARG+1];
184 	int hs;
185 
186 	copytokenrow(&ntr, np->vp);		/* copy macro value */
187 	if (np->ap==NULL)			/* parameterless */
188 		ntokc = 1;
189 	else {
190 		ntokc = gatherargs(trp, atr, &narg);
191 		if (narg<0) {			/* not actually a call (no '(') */
192 			trp->tp++;
193 			return;
194 		}
195 		if (narg != rowlen(np->ap)) {
196 			error(ERROR, "Disagreement in number of macro arguments");
197 			trp->tp->hideset = newhideset(trp->tp->hideset, np);
198 			trp->tp += ntokc;
199 			return;
200 		}
201 		substargs(np, &ntr, atr);	/* put args into replacement */
202 		for (i=0; i<narg; i++) {
203 			dofree(atr[i]->bp);
204 			dofree(atr[i]);
205 		}
206 	}
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) {
211 			if (tp->hideset==0)
212 				tp->hideset = hs;
213 			else
214 				tp->hideset = unionhideset(tp->hideset, hs);
215 		}
216 	}
217 	ntr.tp = ntr.bp;
218 	insertrow(trp, ntokc, &ntr);
219 	trp->tp -= rowlen(&ntr);
220 	dofree(ntr.bp);
221 }
222 
223 /*
224  * Gather an arglist, starting in trp with tp pointing at the macro name.
225  * Return total number of tokens passed, stash number of args found.
226  * trp->tp is not changed relative to the tokenrow.
227  */
228 int
gatherargs(Tokenrow * trp,Tokenrow ** atr,int * narg)229 gatherargs(Tokenrow *trp, Tokenrow **atr, int *narg)
230 {
231 	int parens = 1;
232 	int ntok = 0;
233 	Token *bp, *lp;
234 	Tokenrow ttr;
235 	int ntokp;
236 	int needspace;
237 
238 	*narg = -1;			/* means that there is no macro call */
239 	/* look for the ( */
240 	for (;;) {
241 		trp->tp++;
242 		ntok++;
243 		if (trp->tp >= trp->lp) {
244 			gettokens(trp, 0);
245 			if ((trp->lp-1)->type==END) {
246 				trp->lp -= 1;
247 				trp->tp -= ntok;
248 				return ntok;
249 			}
250 		}
251 		if (trp->tp->type==LP)
252 			break;
253 		if (trp->tp->type!=NL)
254 			return ntok;
255 	}
256 	*narg = 0;
257 	ntok++;
258 	ntokp = ntok;
259 	trp->tp++;
260 	/* search for the terminating ), possibly extending the row */
261 	needspace = 0;
262 	while (parens>0) {
263 		if (trp->tp >= trp->lp)
264 			gettokens(trp, 0);
265 		if (needspace) {
266 			needspace = 0;
267 			makespace(trp);
268 		}
269 		if (trp->tp->type==END) {
270 			trp->lp -= 1;
271 			trp->tp -= ntok;
272 			error(ERROR, "EOF in macro arglist");
273 			return ntok;
274 		}
275 		if (trp->tp->type==NL) {
276 			trp->tp += 1;
277 			adjustrow(trp, -1);
278 			trp->tp -= 1;
279 			makespace(trp);
280 			needspace = 1;
281 			continue;
282 		}
283 		if (trp->tp->type==LP)
284 			parens++;
285 		else if (trp->tp->type==RP)
286 			parens--;
287 		trp->tp++;
288 		ntok++;
289 	}
290 	trp->tp -= ntok;
291 	/* Now trp->tp won't move underneath us */
292 	lp = bp = trp->tp+ntokp;
293 	for (; parens>=0; lp++) {
294 		if (lp->type == LP) {
295 			parens++;
296 			continue;
297 		}
298 		if (lp->type==RP)
299 			parens--;
300 		if (lp->type==DSHARP)
301 			lp->type = DSHARP1;	/* ## not special in arg */
302 		if ((lp->type==COMMA && parens==0) || (parens<0 && (lp-1)->type!=LP)) {
303 			if (*narg>=NARG-1)
304 				error(FATAL, "Sorry, too many macro arguments");
305 			ttr.bp = ttr.tp = bp;
306 			ttr.lp = lp;
307 			atr[(*narg)++] = normtokenrow(&ttr);
308 			bp = lp+1;
309 		}
310 	}
311 	return ntok;
312 }
313 
314 /*
315  * substitute the argument list into the replacement string
316  *  This would be simple except for ## and #
317  */
318 void
substargs(Nlist * np,Tokenrow * rtr,Tokenrow ** atr)319 substargs(Nlist *np, Tokenrow *rtr, Tokenrow **atr)
320 {
321 	Tokenrow tatr;
322 	Token *tp;
323 	int ntok, argno;
324 
325 	for (rtr->tp=rtr->bp; rtr->tp<rtr->lp; ) {
326 		if (rtr->tp->type==SHARP) {	/* string operator */
327 			tp = rtr->tp;
328 			rtr->tp += 1;
329 			if ((argno = lookuparg(np, rtr->tp))<0) {
330 				error(ERROR, "# not followed by macro parameter");
331 				continue;
332 			}
333 			ntok = 1 + (rtr->tp - tp);
334 			rtr->tp = tp;
335 			insertrow(rtr, ntok, stringify(atr[argno]));
336 			continue;
337 		}
338 		if (rtr->tp->type==NAME
339 		 && (argno = lookuparg(np, rtr->tp)) >= 0) {
340 			if ((rtr->tp+1)->type==DSHARP
341 			 || (rtr->tp!=rtr->bp && (rtr->tp-1)->type==DSHARP))
342 				insertrow(rtr, 1, atr[argno]);
343 			else {
344 				copytokenrow(&tatr, atr[argno]);
345 				expandrow(&tatr, "<macro>");
346 				insertrow(rtr, 1, &tatr);
347 				dofree(tatr.bp);
348 			}
349 			continue;
350 		}
351 		rtr->tp++;
352 	}
353 }
354 
355 /*
356  * Evaluate the ## operators in a tokenrow
357  */
358 void
doconcat(Tokenrow * trp)359 doconcat(Tokenrow *trp)
360 {
361 	Token *ltp, *ntp;
362 	Tokenrow ntr;
363 	int len;
364 
365 	for (trp->tp=trp->bp; trp->tp<trp->lp; trp->tp++) {
366 		if (trp->tp->type==DSHARP1)
367 			trp->tp->type = DSHARP;
368 		else if (trp->tp->type==DSHARP) {
369 			char tt[128];
370 			ltp = trp->tp-1;
371 			ntp = trp->tp+1;
372 			if (ltp<trp->bp || ntp>=trp->lp) {
373 				error(ERROR, "## occurs at border of replacement");
374 				continue;
375 			}
376 			len = ltp->len + ntp->len;
377 			strncpy((char*)tt, (char*)ltp->t, ltp->len);
378 			strncpy((char*)tt+ltp->len, (char*)ntp->t, ntp->len);
379 			tt[len] = '\0';
380 			setsource("<##>", -1, tt);
381 			maketokenrow(3, &ntr);
382 			gettokens(&ntr, 1);
383 			unsetsource();
384 			if (ntr.lp-ntr.bp!=2 || ntr.bp->type==UNCLASS)
385 				error(WARNING, "Bad token %r produced by ##", &ntr);
386 			ntr.lp = ntr.bp+1;
387 			trp->tp = ltp;
388 			makespace(&ntr);
389 			insertrow(trp, (ntp-ltp)+1, &ntr);
390 			dofree(ntr.bp);
391 			trp->tp--;
392 		}
393 	}
394 }
395 
396 /*
397  * tp is a potential parameter name of macro mac;
398  * look it up in mac's arglist, and if found, return the
399  * corresponding index in the argname array.  Return -1 if not found.
400  */
401 int
lookuparg(Nlist * mac,Token * tp)402 lookuparg(Nlist *mac, Token *tp)
403 {
404 	Token *ap;
405 
406 	if (tp->type!=NAME || mac->ap==NULL)
407 		return -1;
408 	for (ap=mac->ap->bp; ap<mac->ap->lp; ap++) {
409 		if (ap->len==tp->len && strncmp((char*)ap->t,(char*)tp->t,ap->len)==0)
410 			return ap - mac->ap->bp;
411 	}
412 	return -1;
413 }
414 
415 /*
416  * Return a quoted version of the tokenrow (from # arg)
417  */
418 #define	STRLEN	512
419 Tokenrow *
stringify(Tokenrow * vp)420 stringify(Tokenrow *vp)
421 {
422 	static Token t = { STRING };
423 	static Tokenrow tr = { &t, &t, &t+1, 1 };
424 	Token *tp;
425 	uchar s[STRLEN];
426 	uchar *sp = s, *cp;
427 	int i, instring;
428 
429 	*sp++ = '"';
430 	for (tp = vp->bp; tp < vp->lp; tp++) {
431 		instring = tp->type==STRING || tp->type==CCON;
432 		if (sp+2*tp->len >= &s[STRLEN-10]) {
433 			error(ERROR, "Stringified macro arg is too long");
434 			break;
435 		}
436 		if (tp->wslen && (tp->flag&XPWS)==0)
437 			*sp++ = ' ';
438 		for (i=0, cp=tp->t; i<tp->len; i++) {
439 			if (instring && (*cp=='"' || *cp=='\\'))
440 				*sp++ = '\\';
441 			*sp++ = *cp++;
442 		}
443 	}
444 	*sp++ = '"';
445 	*sp = '\0';
446 	sp = s;
447 	t.len = strlen((char*)sp);
448 	t.t = newstring(sp, t.len, 0);
449 	return &tr;
450 }
451 
452 /*
453  * expand a builtin name
454  */
455 void
builtin(Tokenrow * trp,int biname)456 builtin(Tokenrow *trp, int biname)
457 {
458 	char *op;
459 	Token *tp;
460 	Source *s;
461 
462 	tp = trp->tp;
463 	trp->tp++;
464 	/* need to find the real source */
465 	s = cursource;
466 	while (s && s->fd==-1)
467 		s = s->next;
468 	if (s==NULL)
469 		s = cursource;
470 	/* most are strings */
471 	tp->type = STRING;
472 	if (tp->wslen) {
473 		*outbufp++ = ' ';
474 		tp->wslen = 1;
475 	}
476 	op = outbufp;
477 	*op++ = '"';
478 	switch (biname) {
479 
480 	case KLINENO:
481 		tp->type = NUMBER;
482 		op = outnum(op-1, s->line);
483 		break;
484 
485 	case KFILE: {
486 		char *src = s->filename;
487 		while ((*op++ = *src++) != 0)
488 			if (src[-1] == '\\')
489 				*op++ = '\\';
490 		op--;
491 		break;
492 		}
493 
494 	case KDATE:
495 		strncpy(op, curtime+4, 7);
496 		strncpy(op+7, curtime+20, 4);
497 		op += 11;
498 		break;
499 
500 	case KTIME:
501 		strncpy(op, curtime+11, 8);
502 		op += 8;
503 		break;
504 
505 	default:
506 		error(ERROR, "cpp botch: unknown internal macro");
507 		return;
508 	}
509 	if (tp->type==STRING)
510 		*op++ = '"';
511 	tp->t = (uchar*)outbufp;
512 	tp->len = op - outbufp;
513 	outbufp = op;
514 }
515