summaryrefslogtreecommitdiff
path: root/usr/src/lib/libpp/common/ppexpr.c
blob: 5fd93afde5f4907a63854fc0cc50de4fa0934176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*           Copyright (c) 1986-2007 AT&T Knowledge Ventures            *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                      by AT&T Knowledge Ventures                      *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                                                                      *
***********************************************************************/
#pragma prototyped
/*
 * Glenn Fowler
 * AT&T Research
 *
 * preprocessor expression evaluation support
 */

#include "pplib.h"

#include <regex.h>

#define lex(c)		((((c)=peektoken)>=0?(peektoken=(-1)):((c)=pplex())),(c))
#define unlex(c)	(peektoken=(c))

static int		peektoken;	/* expression lookahead token	*/
static char*		errmsg;		/* subexpr() error message	*/

/*
 * exists predicate evaluation
 */

static int
exists(int op, char* pred, register char* args)
{
	register int	c;
	register int	type;
	char*		pptoken;
	long		state;
	char		file[MAXTOKEN + 1];

	state = (pp.state & ~DISABLE);
	PUSH_STRING(args);
	pptoken = pp.token;
	pp.token = file;
	pp.state |= HEADER|PASSEOF;
	type = pplex();
	pp.state &= ~HEADER;
	pp.token = pptoken;
	switch (type)
	{
	case T_STRING:
	case T_HEADER:
		break;
	default:
		error(1, "%s: \"...\" or <...> argument expected", pred);
		c = 0;
		goto done;
	}
	if (op == X_EXISTS)
	{
		if ((c = pplex()) == ',')
		{
			while ((c = pplex()) == T_STRING)
			{
				if (pathaccess(pp.path, pp.token, file, NiL, 0))
				{
					pathcanon(pp.path, 0);
					message((-2, "%s: %s found", pred, pp.path));
					c = 1;
					goto done;
				}
				if ((c = pplex()) != ',') break;
			}
			if (c) error(1, "%s: \"...\" arguments expected", pred);
			strcpy(pp.path, file);
			message((-2, "%s: %s not found", pred, file));
			c = 0;
		}
		else c = ppsearch(file, type, SEARCH_EXISTS) >= 0;
	}
	else
	{
		register struct ppfile*	fp;

		fp = ppsetfile(file);
		c = fp->flags || fp->guard == INC_IGNORE;
	}
 done:
	while (pplex());
	pp.state = state;
	return c;
}

/*
 * strcmp/match predicate evaluation
 */

static int
compare(char* pred, char* args, int match)
{
	register int	c;
	char*		pptoken;
	long		state;
	regex_t		re;
	char		tmp[MAXTOKEN + 1];

	state = (pp.state & ~DISABLE);
	PUSH_STRING(args);
	pp.state |= PASSEOF;
	pptoken = pp.token;
	pp.token = tmp;
	if (!pplex())
		goto bad;
	pp.token = pptoken;
	if (pplex() != ',' || !pplex())
		goto bad;
	if (!match)
		c = strcmp(tmp, pp.token);
	else if ((c = regcomp(&re, pp.token, REG_AUGMENTED|REG_LENIENT|REG_NULL)) || (c = regexec(&re, tmp, NiL, 0, 0)) && c != REG_NOMATCH)
		regfatal(&re, 3, c);
	else
	{
		c = !c;
		regfree(&re);
	}
	if ((pp.state & PASSEOF) && pplex())
		goto bad;
	pp.state = state;
	return c;
 bad:
	pp.token = pptoken;
	error(2, "%s: 2 arguments expected", pred);
	while (pplex());
	pp.state = state;
	return 0;
}

/*
 * #if predicate parse and evaluation
 */

static int
predicate(int warn)
{
	register char*			args;
	register struct pplist*		p;
	register struct ppsymbol*	sym;
	register int			type;
	int				index;

	static char			pred[MAXID + 1];

	/*
	 * first gather the args
	 */

	index = (int)hashref(pp.strtab, pp.token);
	if (warn && peekchr() != '(') switch (index)
	{
	case X_DEFINED:
	case X_EXISTS:
	case X_INCLUDED:
	case X_MATCH:
	case X_NOTICED:
	case X_OPTION:
	case X_SIZEOF:
	case X_STRCMP:
		break;
	default:
		if (pp.macref) pprefmac(pp.token, REF_IF);
		return 0;
	}
	strcpy(pred, pp.token);
	pp.state |= DISABLE;
	type = pppredargs();
	pp.state &= ~DISABLE;
	switch (type)
	{
	case T_ID:
	case T_STRING:
		break;
	default:
		unlex(type);
		/*FALLTHROUGH*/
	case 0:
		if (index && !(pp.state & STRICT))
			error(1, "%s: predicate argument expected", pred);
		if (pp.macref) pprefmac(pred, REF_IF);
		return 0;
	}
	args = pp.args;

	/*
	 * now evaluate
	 */

	debug((-6, "pred=%s args=%s", pred, args));
	if ((pp.state & STRICT) && !(pp.mode & HOSTED)) switch (index)
	{
	case X_DEFINED:
	case X_SIZEOF:
		break;
	default:
		error(1, "%s(%s): non-standard predicate test", pred, args);
		return 0;
	}
	switch (index)
	{
	case X_DEFINED:
		if (type != T_ID) error(1, "%s: identifier argument expected", pred);
		else if ((sym = pprefmac(args, REF_IF)) && sym->macro) return 1;
		else if (args[0] == '_' && args[1] == '_' && !strncmp(args, "__STDPP__", 9))
		{
			if (pp.hosted == 1 && pp.in->prev->type == IN_FILE)
			{
				pp.mode |= HOSTED;
				pp.flags |= PP_hosted;
			}
			return *(args + 9) ? (int)hashref(pp.strtab, args + 9) : 1;
		}
		break;
	case X_EXISTS:
	case X_INCLUDED:
		return exists(index, pred, args);
	case X_MATCH:
	case X_STRCMP:
		return compare(pred, args, index == X_MATCH);
	case X_NOTICED:
		if (type != T_ID) error(1, "%s: identifier argument expected", pred);
		else if (((sym = pprefmac(args, REF_IF)) || (sym = ppsymref(pp.symtab, args))) && (sym->flags & SYM_NOTICED)) return 1;
		break;
	case X_OPTION:
		return ppoption(args);
	case X_SIZEOF:
		error(2, "%s invalid in #%s expressions", pred, dirname(IF));
		break;
	default:
		if (warn && !(pp.mode & HOSTED) && (sym = ppsymref(pp.symtab, pred)) && (sym->flags & SYM_PREDICATE))
			error(1, "use #%s(%s) to disambiguate", pred, args);
		if (p = (struct pplist*)hashget(pp.prdtab, pred))
		{
			if (!*args) return 1;
			while (p)
			{
				if (streq(p->value, args)) return 1;
				p = p->next;
			}
		}
		break;
	}
	return 0;
}

/*   
 * evaluate a long integer subexpression with precedence
 * taken from the library routine streval()
 * may be called recursively
 *
 * NOTE: all operands are evaluated as both the parse
 *	 and evaluation are done on the fly
 */

static long
subexpr(register int precedence, int* pun)
{
	register int		c;
	register long		n;
	register long		x;
	register int		operand = 1;
	int			un = 0;
	int			xn;

	switch (lex(c))
	{
	case 0:
	case '\n':
		unlex(c);
		if (!errmsg && !(pp.mode & INACTIVE)) errmsg = "more tokens expected";
		return 0;
	case '-':
		n = -subexpr(13, &un);
		break;
	case '+':
		n = subexpr(13, &un);
		break;
	case '!':
		n = !subexpr(13, &un);
		break;
	case '~':
		n = ~subexpr(13, &un);
		break;
	default:
		unlex(c);
		n = 0;
		operand = 0;
		break;
	}
	un <<= 1;
	for (;;)
	{
		switch (lex(c))
		{
		case 0:
		case '\n':
			goto done;
		case ')':
			if (!precedence)
			{
				if (!errmsg && !(pp.mode & INACTIVE)) errmsg = "too many )'s";
				return 0;
			}
			goto done;
		case '(':
			n = subexpr(1, &un);
			if (lex(c) != ')')
			{
				unlex(c);
				if (!errmsg && !(pp.mode & INACTIVE)) errmsg = "closing ) expected";
				return 0;
			}
		gotoperand:
			if (operand)
			{
				if (!errmsg && !(pp.mode & INACTIVE)) errmsg = "operator expected";
				return 0;
			}
			operand = 1;
			un <<= 1;
			continue;
		case '?':
			if (precedence > 1) goto done;
			un = 0;
			if (lex(c) == ':')
			{
				if (!n) n = subexpr(2, &un);
				else
				{
					x = pp.mode;
					pp.mode |= INACTIVE;
					subexpr(2, &xn);
					pp.mode = x;
				}
			}
			else
			{
				unlex(c);
				x = subexpr(2, &xn);
				if (lex(c) != ':')
				{
					unlex(c);
					if (!errmsg && !(pp.mode & INACTIVE)) errmsg = ": expected for ? operator";
					return 0;
				}
				if (n)
				{
					n = x;
					un = xn;
					subexpr(2, &xn);
				}
				else n = subexpr(2, &un);
			}
			break;
		case ':':
			goto done;
		case T_ANDAND:
		case T_OROR:
			xn = (c == T_ANDAND) ? 4 : 3;
			if (precedence >= xn) goto done;
			if ((n != 0) == (c == T_ANDAND)) n = subexpr(xn, &un) != 0;
			else
			{
				x = pp.mode;
				pp.mode |= INACTIVE;
				subexpr(xn, &un);
				pp.mode = x;
			}
			un = 0;
			break;
		case '|':
			if (precedence > 4) goto done;
			n |= subexpr(5, &un);
			break;
		case '^':
			if (precedence > 5) goto done;
			n ^= subexpr(6, &un);
			break;
		case '&':
			if (precedence > 6) goto done;
			n &= subexpr(7, &un);
			break;
		case T_EQ:
		case T_NE:
			if (precedence > 7) goto done;
			n = (n == subexpr(8, &un)) == (c == T_EQ);
			un = 0;
			break;
		case '<':
		case T_LE:
		case T_GE:
		case '>':
			if (precedence > 8) goto done;
			x = subexpr(9, &un);
			switch (c)
			{
			case '<':
				switch (un)
				{
				case 01:
					n = n < (unsigned long)x;
					break;
				case 02:
					n = (unsigned long)n < x;
					break;
				case 03:
					n = (unsigned long)n < (unsigned long)x;
					break;
				default:
					n = n < x;
					break;
				}
				break;
			case T_LE:
				switch (un)
				{
				case 01:
					n = n <= (unsigned long)x;
					break;
				case 02:
					n = (unsigned long)n <= x;
					break;
				case 03:
					n = (unsigned long)n <= (unsigned long)x;
					break;
				default:
					n = n <= x;
					break;
				}
				break;
			case T_GE:
				switch (un)
				{
				case 01:
					n = n >= (unsigned long)x;
					break;
				case 02:
					n = (unsigned long)n >= x;
					break;
				case 03:
					n = (unsigned long)n >= (unsigned long)x;
					break;
				default:
					n = n >= x;
					break;
				}
				break;
			case '>':
				switch (un)
				{
				case 01:
					n = n > (unsigned long)x;
					break;
				case 02:
					n = (unsigned long)n > x;
					break;
				case 03:
					n = (unsigned long)n > (unsigned long)x;
					break;
				default:
					n = n > x;
					break;
				}
				break;
			}
			un = 0;
			break;
		case T_LSHIFT:
		case T_RSHIFT:
			if (precedence > 9) goto done;
			x = subexpr(10, &un);
			if (c == T_LSHIFT) n <<= x;
			else n >>= x;
			un >>= 1;
			break;
		case '+':
		case '-':
			if (precedence > 10) goto done;
			x = subexpr(11, &un);
			if (c == '+') n += x;
			else n -= x;
			break;
		case '*':
		case '/':
		case '%':
			if (precedence > 11) goto done;
			x = subexpr(12, &un);
			if (c == '*') n *= x;
			else if (x == 0)
			{
				if (!errmsg && !(pp.mode & INACTIVE)) errmsg = "divide by zero";
				return 0;
			}
			else if (c == '/') n /= x;
			else n %= x;
			break;
		case '#':
			pp.state |= DISABLE;
			c = pplex();
			pp.state &= ~DISABLE;
			if (c != T_ID)
			{
				if (!errmsg && !(pp.mode & INACTIVE)) errmsg = "# must precede a predicate identifier";
				return 0;
			}
			n = predicate(0);
			goto gotoperand;
		case T_ID:
			n = predicate(1);
			goto gotoperand;
		case T_CHARCONST:
			c = *(pp.toknxt - 1);
			*(pp.toknxt - 1) = 0;
			n = chrtoi(pp.token + 1);
			*(pp.toknxt - 1) = c;
			if (n & ~((1<<CHAR_BIT)-1))
			{
				if (!(pp.mode & HOSTED))
					error(1, "'%s': multi-character character constants are not portable", pp.token);
			}
#if CHAR_MIN < 0
			else n = (char)n;
#endif
			goto gotoperand;
		case T_DECIMAL_U:
		case T_DECIMAL_UL:
		case T_OCTAL_U:
		case T_OCTAL_UL:
		case T_HEXADECIMAL_U:
		case T_HEXADECIMAL_UL:
			un |= 01;
			/*FALLTHROUGH*/
		case T_DECIMAL:
		case T_DECIMAL_L:
		case T_OCTAL:
		case T_OCTAL_L:
		case T_HEXADECIMAL:
		case T_HEXADECIMAL_L:
			n = strtoul(pp.token, NiL, 0);
			if ((unsigned long)n > LONG_MAX) un |= 01;
			goto gotoperand;
		case T_WCHARCONST:
			n = chrtoi(pp.token);
			goto gotoperand;
		default:
			if (!errmsg && !(pp.mode & INACTIVE)) errmsg = "invalid token";
			return 0;
		}
		if (errmsg) return 0;
		if (!operand) goto nooperand;
	}
 done:
	unlex(c);
	if (!operand)
	{
	nooperand:
		if (!errmsg && !(pp.mode & INACTIVE)) errmsg = "operand expected";
		return 0;
	}
	if (un) *pun |= 01;
	return n;
}

/*
 * preprocessor expression evaluator using modified streval(3)
 * *pun!=0 if result is unsigned
 */

long
ppexpr(int* pun)
{
	long	n;
	int	opeektoken;
	long	ppstate;

	ppstate = (pp.state & (CONDITIONAL|DISABLE|NOSPACE|STRIP));
	pp.state &= ~(DISABLE|STRIP);
	pp.state |= CONDITIONAL|NOSPACE;
	opeektoken = peektoken;
	peektoken = -1;
	*pun = 0;
	n = subexpr(0, pun);
	if (peektoken == ':' && !errmsg && !(pp.mode & INACTIVE)) errmsg = "invalid use of :";
	if (errmsg)
	{
		error(2, "%s in expression", errmsg);
		errmsg = 0;
		n = 0;
	}
	peektoken = opeektoken;
	pp.state &= ~(CONDITIONAL|NOSPACE);
	pp.state |= ppstate;
	if (*pun) debug((-4, "ppexpr() = %luU", n));
	else debug((-4, "ppexpr() = %ld", n));
	return n;
}

/*
 * return non-zero if option s is set
 */

int
ppoption(char* s)
{
	switch ((int)hashget(pp.strtab, s))
	{
	case X_ALLMULTIPLE:
		return pp.mode & ALLMULTIPLE;
	case X_BUILTIN:
		return pp.mode & BUILTIN;
	case X_CATLITERAL:
		return pp.mode & CATLITERAL;
	case X_COMPATIBILITY:
		return pp.state & COMPATIBILITY;
	case X_DEBUG:
		return -error_info.trace;
	case X_ELSEIF:
		return pp.option & ELSEIF;
	case X_FINAL:
		return pp.option & FINAL;
	case X_HOSTDIR:
		return pp.mode & HOSTED;
	case X_HOSTED:
		return pp.flags & PP_hosted;
	case X_INITIAL:
		return pp.option & INITIAL;
	case X_KEYARGS:
		return pp.option & KEYARGS;
	case X_LINEBASE:
		return pp.flags & PP_linebase;
	case X_LINEFILE:
		return pp.flags & PP_linefile;
	case X_LINETYPE:
		return pp.flags & PP_linetype;
	case X_PLUSCOMMENT:
		return pp.option & PLUSCOMMENT;
	case X_PLUSPLUS:
		return pp.option & PLUSPLUS;
	case X_PLUSSPLICE:
		return pp.option & PLUSSPLICE;
	case X_PRAGMAEXPAND:
		return pp.option & PRAGMAEXPAND;
	case X_PREDEFINED:
		return pp.option & PREDEFINED;
	case X_PREFIX:
		return pp.option & PREFIX;
	case X_PROTOTYPED:
		return pp.option & PROTOTYPED;
	case X_READONLY:
		return pp.mode & READONLY;
	case X_REGUARD:
		return pp.option & REGUARD;
	case X_SPACEOUT:
		return pp.state & SPACEOUT;
	case X_SPLICECAT:
		return pp.option & SPLICECAT;
	case X_SPLICESPACE:
		return pp.option & SPLICESPACE;
	case X_STRICT:
		return pp.state & STRICT;
	case X_STRINGSPAN:
		return pp.option & STRINGSPAN;
	case X_STRINGSPLIT:
		return pp.option & STRINGSPLIT;
	case X_TEST:
		return pp.test;
	case X_TEXT:
		return !(pp.state & NOTEXT);
	case X_TRANSITION:
		return pp.state & TRANSITION;
	case X_TRUNCATE:
		return pp.truncate;
	case X_WARN:
		return pp.state & WARN;
	default:
		if (pp.state & WARN) error(1, "%s: unknown option name", s);
		return 0;
	}
}