1 #include <sys/cdefs.h>
2 
3 __SCCSID("@(#)yylex.c	1.4 10/15/86");
4 __RCSID("$MirOS: src/libexec/cpp/yylex.c,v 1.4 2008/11/22 13:06:44 tg Exp $");
5 
6 #define isid(a)  ((fastab+COFF)[a]&IB)
7 #define IB 1
8 /*	#if '\377' < 0		it would be nice if this worked properly!!!!! */
9 #if pdp11 | vax | mc68000 | tahoe
10 #define COFF 128
11 #else
12 #define COFF 0
13 #endif
14 
yylex()15 yylex() {
16 	static int ifdef=0;
17 	static char *op2[]={"||",  "&&" , ">>", "<<", ">=", "<=", "!=", "=="};
18 	static int  val2[]={OROR, ANDAND,  RS,   LS,   GE,   LE,   NE,   EQ};
19 	static char *opc="b\bt\tn\nf\fr\r\\\\";
20 	extern char fastab[];
21 	extern char *outp,*inp,*newp; extern int flslvl;
22 	register char savc, *s; char *skipbl(); int val;
23 	register char **p2;
24 	struct symtab {
25 		char *name;
26 		char *value;
27 	} *sp, *lookup();
28 
29 for (;;) {
30 	extern int passcom;		/* this crap makes #if's work */
31 	int opt_passcom = passcom;	/* even with -C option */
32 	passcom = 0;			/* (else comments make syntax errs) */
33 	newp=skipbl(newp);
34 	passcom = opt_passcom;		/* nb: lint uses -C so its useful! */
35 	if (*inp=='\n') return(stop);	/* end of #if */
36 	savc= *newp; *newp='\0';
37 	for (p2=op2+8; --p2>=op2; )	/* check 2-char ops */
38 		if (0==strcmp(*p2,inp)) {val=val2[p2-op2]; goto ret;}
39 	s="+-*/%<>&^|?:!~(),";	/* check 1-char ops */
40 	while (*s) if (*s++== *inp) {val= *--s; goto ret;}
41 	if (*inp<='9' && *inp>='0') {/* a number */
42 		if (*inp=='0') yylval= (inp[1]=='x' || inp[1]=='X') ?
43 			tobinary(inp+2,16) : tobinary(inp+1,8);
44 		else yylval=tobinary(inp,10);
45 		val=number;
46 	} else if (isid(*inp)) {
47 		if (0==strcmp(inp,"defined")) {ifdef=1; ++flslvl; val=DEFINED;}
48 		else {
49 			sp=lookup(inp,-1); if (ifdef!=0) {ifdef=0; --flslvl;}
50 			yylval= (sp->value==0) ? 0 : 1;
51 			val=number;
52 		}
53 	} else 	if (*inp=='\'') {/* character constant */
54 		val=number;
55 		if (inp[1]=='\\') {/* escaped */
56 			char c; if (newp[-1]=='\'') newp[-1]='\0';
57 			s=opc;
58 			while (*s) if (*s++!=inp[2]) ++s; else {yylval= *s; goto ret;}
59 			if (inp[2]<='9' && inp[2]>='0') yylval=c=tobinary(inp+2,8);
60 			else yylval=inp[2];
61 		} else yylval=inp[1];
62 	} else if (0==strcmp("\\\n",inp)) {*newp=savc; continue;}
63 	else {
64 		*newp=savc; pperror("Illegal character %c in preprocessor if", *inp);
65 		continue;
66 	}
67 ret:
68 	*newp=savc; outp=inp=newp; return(val);
69 }
70 }
71 
tobinary(st,b)72 tobinary(st, b) char *st; {
73 	int n, c, t;
74 	char *s;
75 	n=0;
76 	s=st;
77 	while (c = *s++) {
78 	switch(c) {
79 		case '0': case '1': case '2': case '3': case '4':
80 		case '5': case '6': case '7': case '8': case '9':
81 			t = c-'0'; break;
82 		case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
83 			t = c-'a'+10; if (b>10) break;
84 		case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
85 			t = c - 'A'+10; if (b>10) break;
86 		default:
87 			t = -1;
88 			if ( c=='l' || c=='L') if (*s=='\0') break;
89 			pperror("Illegal number %s", st);
90 	}
91 	if (t<0) break;
92 	n = n*b+t;
93 	}
94 return(n);
95 }
96