1 /* @(#)yylex.c	1.9 19/08/29 2010-2019 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)yylex.c	1.9 19/08/29 2010-2019 J. Schilling";
6 #endif
7 /*
8  * This implementation is based on the UNIX 32V release from 1978
9  * with permission from Caldera Inc.
10  *
11  * Copyright (c) 2010-2019 J. Schilling
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the copyright holder nor the names of contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 /*
39  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions are
43  * met:
44  * 1. Redistributions of source code and documentation must retain the above
45  *    copyright notice, this list of conditions and the following
46  *    disclaimer.
47  *
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  *
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:  This product includes
54  *    software developed or owned by Caldera International, Inc.
55  *
56  * 4. Neither the name of Caldera International, Inc. nor the names of other
57  *    contributors may be used to endorse or promote products derived from
58  *    this software without specific prior written permission.
59  *
60  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
61  * INTERNATIONAL, INC.  AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
62  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
63  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
64  * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR
65  * ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
69  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
70  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
71  * POSSIBILITY OF SUCH DAMAGE.
72  */
73 
74 #include <schily/string.h>
75 #include "cpp.h"
76 
77 /*
78  * XXX This block should be moved to cpp.h, it is douplicated in cpp.c
79  */
80 #define isid(a)  ((fastab+COFF)[(int)a]&IB)
81 #define IB 1
82 /*	#if '\377' < 0		it would be nice if this worked properly!!!!! */
83 #if pdp11 | vax | '\377' < 0
84 #define COFF 128
85 #else
86 #define COFF 0
87 #endif
88 
89 EXPORT	int	yylex	 __PR((void));
90 LOCAL	int	tobinary __PR((char *, int));
91 
92 int
yylex()93 yylex()
94 {
95 	static int ifdef=0;
96 	static char *op2[]={"||",  "&&" , ">>", "<<", ">=", "<=", "!=", "=="};
97 	static int  val2[]={OROR, ANDAND,  RS,   LS,   GE,   LE,   NE,   EQ};
98 	static char *opc="b\bt\tn\nf\fr\r\\\\";
99 	extern char fastab[];
100 	extern char *outptr,*inptr,*newp; extern int flslvl;
101 	extern int char_is_signed;
102 	register char savc, *s;
103 	int val;
104 	register char **p2;
105 	struct symtab *sp;
106 
107 for (;;) {
108 	newp=skipbl(newp);
109 	if (*inptr=='\n') return(stop);	/* end of #if */
110 	savc= *newp; *newp='\0';
111 	for (p2=op2+8; --p2>=op2; )	/* check 2-char ops */
112 		if (0==strcmp(*p2,inptr)) {val=val2[p2-op2]; goto ret;}
113 	s="+-*/%<>&^|?:!~(),";	/* check 1-char ops */
114 	while (*s) if (*s++== *inptr) {val= *--s; goto ret;}
115 	if (*inptr<='9' && *inptr>='0') {/* a number */
116 		if (*inptr=='0') yylval= (inptr[1]=='x' || inptr[1]=='X') ?
117 			tobinary(inptr+2,16) : tobinary(inptr+1,8);
118 		else yylval=tobinary(inptr,10);
119 		val=number;
120 	} else if (isid(*inptr)) {
121 		if (0==strcmp(inptr,"defined")) {ifdef=1; ++flslvl; val=DEFINED;}
122 		else {
123 			sp=lookup(inptr,-1); if (ifdef!=0) {ifdef=0; --flslvl;}
124 			yylval= (sp->value==0) ? 0 : 1;
125 			val=number;
126 		}
127 	} else 	if (*inptr=='\'') {/* character constant */
128 		val=number;
129 		if (inptr[1]=='\\') {/* escaped */
130 			char		c;
131 #if	defined(__STDC__) || defined(CHAR_IS_UNSIGNED)
132 			signed char	sc;
133 #else
134 			char		sc;
135 #endif
136 			unsigned char	uc;
137 
138 			if (newp[-1] == '\'')
139 				newp[-1] = '\0';
140 			s=opc;
141 			while (*s) if (*s++!=inptr[2]) ++s; else {yylval= *s; goto ret;}
142 			if (inptr[2]<='9' && inptr[2]>='0') {
143 				if (char_is_signed < 0)
144 					yylval= c = tobinary(inptr+2, 8);
145 				else if (char_is_signed == 0)
146 					yylval= uc = tobinary(inptr+2, 8);
147 				else
148 					yylval= sc = tobinary(inptr+2, 8);
149 			} else {
150 				yylval=inptr[2];
151 			}
152 		} else yylval=inptr[1];
153 	} else if (0==strcmp("\\\n",inptr)) {*newp=savc; continue;}
154 	else {
155 		*newp=savc; pperror("Illegal character %c in preprocessor if", *inptr);
156 		continue;
157 	}
158 ret:
159 	*newp=savc; outptr=inptr=newp; return(val);
160 }
161 }
162 
163 LOCAL int
tobinary(st,b)164 tobinary(st, b)
165 	char	*st;
166 	int	b;
167 {
168 	int n, c, t;
169 	char *s;
170 	n=0;
171 	s=st;
172 	while ((c = *s++) != '\0') {
173 	switch(c) {
174 		case '0': case '1': case '2': case '3': case '4':
175 		case '5': case '6': case '7': case '8': case '9':
176 			t = c-'0'; break;
177 		case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
178 			t = c-'a'; if (b>10) break;
179 		case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
180 			t = c - 'A'; if (b>10) break;
181 		default:
182 			t = -1;
183 			if ( c=='l' || c=='L') if (*s=='\0') break;
184 			pperror("Illegal number %s", st);
185 	}
186 	if (t<0) break;
187 	n = n*b+t;
188 	}
189 return(n);
190 }
191