1 %{
2 /*-
3  * This code contains changes by
4  *      Gunnar Ritter, Freiburg i. Br., Germany, 2002. All rights reserved.
5  *
6  * Conditions 1, 2, and 4 and the no-warranty notice below apply
7  * to these changes.
8  *
9  *
10  * Copyright (c) 1991
11  * 	The Regents of the University of California.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  * 	This product includes software developed by the University of
24  * 	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *
42  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  *   Redistributions of source code and documentation must retain the
48  *    above copyright notice, this list of conditions and the following
49  *    disclaimer.
50  *   Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  *   All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *      This product includes software developed or owned by Caldera
56  *      International, Inc.
57  *   Neither the name of Caldera International, Inc. nor the names of
58  *    other contributors may be used to endorse or promote products
59  *    derived from this software without specific prior written permission.
60  *
61  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
62  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
63  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
64  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
66  * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
67  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
68  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
69  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
70  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
71  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
72  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73  */
74 
75 /*	from 4.4BSD /usr/src/old/awk/awk.lx.l	4.5 (Berkeley) 4/17/91	*/
76 /*	Sccsid @(#)awk.lx.l	1.13 (gritter) 6/18/05>	*/
77 %}
78 
79 %X str chc reg esc comment
80 
81 %{
82 #include	<string.h>
83 #include	<unistd.h>
84 #include	"awk.h"
85 #include	"awk.def"
86 extern void	*yylval;
87 extern int	mustfld;
88 extern int	ldbg;
89 extern char	*lexprog;
90 
91 #define	cast	(void *)(intptr_t)
92 
93 #ifdef	FLEX_SCANNER
94 #undef YY_INPUT
95 #define YY_INPUT(buf,result,max_size)				\
96 {								\
97 	if ( lexprog )						\
98 		{						\
99 		result = strlen( lexprog );			\
100 		if ( result > max_size )			\
101 			{					\
102 			result = max_size;			\
103 			strncpy( buf, lexprog, result );	\
104 			}					\
105 		else						\
106 			strcpy( buf, lexprog );			\
107 		lexprog += result;				\
108 		}						\
109 	else							\
110 		result = read( fileno(yyin), buf, max_size );	\
111 }
112 #else	/* !FLEX_SCANNER */
113 #undef	input
input(void)114 int	input(void)
115 {
116 	register int c;
117 	extern char	*lexprog;
118 
119 	if (yysptr > yysbuf)
120 		c = U(*--yysptr) & 0377;
121 	else if (yyin == NULL)
122 		c = *lexprog++ & 0377;
123 	else
124 		c = getc(yyin);
125 	if (c == '\n')
126 		yylineno++;
127 	else if (c == EOF)
128 		c = 0;
129 	return(c);
130 }
131 #endif	/* !FLEX_SCANNER */
132 
133 long long	lineno	= 1;
134 #define	RETURN(x)	{if (ldbg) ptoken(x); return(x); }
135 #define	CADD	cbuf[clen++]=yytext[0]; if(clen>=CBUFLEN-1) {yyerror("string too long"); BEGIN INITIAL;}
136 #define	CBUFLEN	800
137 char	cbuf[CBUFLEN];
138 int	clen, cflag;
139 
140 static void	ptoken(int);
141 %}
142 
143 A	[a-zA-Z_]
144 B	[a-zA-Z0-9_]
145 D	[0-9]
146 WS	[ \t]
147 
148 %%
149 	static int sc_flag = 0;
150 
151 	if ( sc_flag ) {
152 		BEGIN INITIAL;
153 		sc_flag = 0;
154 		RETURN('}');
155 	}
156 
157 ^\n		lineno++;
158 ^{WS}*#.*\n	lineno++;	/* strip comment lines */
159 {WS}		;
160 <INITIAL,reg>"\\"\n	lineno++;
161 "||"		RETURN(BOR);
162 BEGIN	RETURN(XBEGIN);
163 END		RETURN(XEND);
164 PROGEND	RETURN(EOF);
165 "&&"		RETURN(AND);
166 "!"		RETURN(NOT);
167 "!="		{ yylval = cast NE; RETURN(RELOP); }
168 "~"		{ yylval = cast MATCH; RETURN(MATCHOP); }
169 "!~"		{ yylval = cast NOTMATCH; RETURN(MATCHOP); }
170 "<"		{ yylval = cast LT; RETURN(RELOP); }
171 "<="		{ yylval = cast LE; RETURN(RELOP); }
172 "=="		{ yylval = cast EQ; RETURN(RELOP); }
173 ">="		{ yylval = cast GE; RETURN(RELOP); }
174 ">"		{ yylval = cast GT; RETURN(RELOP); }
175 ">>"		{ yylval = cast APPEND; RETURN(RELOP); }
176 "++"		{ yylval = cast INCR; RETURN(INCR); }
177 "--"		{ yylval = cast DECR; RETURN(DECR); }
178 "+="		{ yylval = cast ADDEQ; RETURN(ASGNOP); }
179 "-="		{ yylval = cast SUBEQ; RETURN(ASGNOP); }
180 "*="		{ yylval = cast MULTEQ; RETURN(ASGNOP); }
181 "/="		{ yylval = cast DIVEQ; RETURN(ASGNOP); }
182 "%="		{ yylval = cast MODEQ; RETURN(ASGNOP); }
183 "="		{ yylval = cast ASSIGN; RETURN(ASGNOP); }
184 
185 "$"{D}+	{	if (atoi(yytext+1)==0) {
186 				yylval = lookup("$record", symtab, 0);
187 				RETURN(STRING);
188 			} else {
189 				yylval = fieldadr(atoi(yytext+1));
190 				RETURN(FIELD);
191 			}
192 		}
193 "$"{WS}*	{ RETURN(INDIRECT); }
194 NF		{ mustfld=1; yylval = setsymtab(yytext, EMPTY, 0.0, NUM, symtab); RETURN(VAR); }
195 ({D}+("."?){D}*|"."{D}+)((e|E)("+"|-)?{D}+)?	{
196 		yylval = setsymtab(yytext, EMPTY, atof(yytext), CON|NUM, symtab); RETURN(NUMBER); }
197 "}"{WS}*\n	{ sc_flag = 1; lineno++; RETURN(';'); }
198 "}"		{ sc_flag = 1; RETURN(';'); }
199 ;\n		{ lineno++; RETURN(';'); }
200 \n		{ lineno++; RETURN(NL); }
201 while	RETURN(WHILE);
202 for		RETURN(FOR);
203 if		RETURN(IF);
204 else		RETURN(ELSE);
205 next		RETURN(NEXT);
206 exit		RETURN(EXIT);
207 break	RETURN(BREAK);
208 continue	RETURN(CONTINUE);
209 print	{ yylval = cast PRINT; RETURN(PRINT); }
210 printf	{ yylval = cast PRINTF; RETURN(PRINTF); }
211 sprintf	{ yylval = cast SPRINTF; RETURN(SPRINTF); }
212 split	{ yylval = cast SPLIT; RETURN(SPLIT); }
213 substr	RETURN(SUBSTR);
214 index	RETURN(INDEX);
215 in		RETURN(IN);
216 getline	RETURN(GETLINE);
217 length	{ yylval = cast FLENGTH; RETURN(FNCN); }
218 log		{ yylval = cast FLOG; RETURN(FNCN); }
219 int		{ yylval = cast FINT; RETURN(FNCN); }
220 exp		{ yylval = cast FEXP; RETURN(FNCN); }
221 sqrt		{ yylval = cast FSQRT; RETURN(FNCN); }
222 {A}{B}*	{ yylval = setsymtab(yytext, tostring(""), 0.0, STR|NUM, symtab); RETURN(VAR); }
223 \"		{ BEGIN str; clen=0; }
224 
225 #		{ BEGIN comment; }
226 <comment>\n	{ BEGIN INITIAL; lineno++; RETURN(NL); }
227 <comment>.	;
228 
229 .		{ yylval = cast yytext[0]; RETURN(yytext[0]); }
230 
231 <reg>"["	{ BEGIN chc; CADD; }
232 <reg>"[^"	{ BEGIN chc; CADD; }
233 <reg>"\\"	{ BEGIN esc; CADD; }
234 <reg>"/"	{ BEGIN INITIAL;
235 		  cbuf[clen] = 0;
236 		  yylval = tostring(cbuf);
237 		  unput('/');
238 		  RETURN(REGEXPR); }
239 <reg>\n		{ yyerror("newline in regular expression"); lineno++; BEGIN INITIAL; }
240 <reg>.		{ CADD; }
241 
242 <str>\"		{ char *s; BEGIN INITIAL; cbuf[clen]=0; s = tostring(cbuf);
243 		cbuf[clen] = ' '; cbuf[++clen] = 0;
244 		yylval = setsymtab(cbuf, s, 0.0, CON|STR, symtab); RETURN(STRING); }
245 <str>\n		{ yyerror("newline in string"); lineno++; BEGIN INITIAL; }
246 <str>"\\\""	{ cbuf[clen++]='"'; }
247 <str>"\\"n	{ cbuf[clen++]='\n'; }
248 <str>"\\"t	{ cbuf[clen++]='\t'; }
249 <str>"\\"r	{ cbuf[clen++]='\r'; }
250 <str>"\\"b	{ cbuf[clen++]='\b'; }
251 <str>"\\"f	{ cbuf[clen++]='\f'; }
252 <str>"\\\\"	{ cbuf[clen++]='\\'; }
253 <str>.		{ CADD; }
254 
255 <chc>"\\""]"	{ cbuf[clen++] = '\\'; cbuf[clen++]=']'; }
256 <chc>"]"	{ BEGIN reg; CADD; }
257 <chc>\n		{ yyerror("newline in character class"); lineno++; BEGIN INITIAL; }
258 <chc>.		{ CADD; }
259 <esc>.		{ BEGIN reg; CADD; }
260 
261 %%
262 
263 void
264 startreg(void)
265 {
266 	BEGIN reg;
267 	clen = 0;
268 }
269 
270 static void
271 ptoken(int n)
272 {
273 	extern struct tok {
274 		char *tnm;
275 		int yval;
276 	} tok[];
277 
278 	printf("lex:");
279 	if (n < 128) {
280 		printf(" %c\n",n);
281 		return;
282 	}
283 	if (n <= 256 || n >= LASTTOKEN) {
284 		printf("? %o\n",n);
285 		return;
286 	}
287 	printf(" %s",tok[n-257].tnm);
288 	switch (n) {
289 
290 	case RELOP:
291 	case MATCHOP:
292 	case ASGNOP:
293 	case STRING:
294 	case FIELD:
295 	case VAR:
296 	case NUMBER:
297 	case FNCN:
298 		printf(" (%s)", yytext);
299 		break;
300 
301 	case CHAR:
302 		printf(" (%lo)", (long)yylval);
303 		break;
304 	}
305 	putchar('\n');
306 }
307