xref: /freebsd/usr.sbin/config/lang.l (revision a79b71281cd63ad7a6cc43a6d5673a2510b51630)
1 %{
2 /*-
3  * Copyright (c) 1980, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)lang.l	8.1 (Berkeley) 6/6/93
35  * $FreeBSD$
36  */
37 
38 #include <ctype.h>
39 #include <string.h>
40 #include "y.tab.h"
41 #include "config.h"
42 
43 #define YY_NO_UNPUT
44 
45 /*
46  * Key word table
47  */
48 
49 struct kt {
50 	char *kt_name;
51 	int kt_val;
52 } key_words[] = {
53 	{ "config",	CONFIG },
54 	{ "cpu",	CPU },
55 	{ "device",	DEVICE },
56 	{ "hints",	HINTS },
57 	{ "ident",	IDENT },
58 	{ "machine",	ARCH }, /* MACHINE is defined in /sys/param.h */
59 	{ "makeoptions", MAKEOPTIONS },
60 	{ "maxusers",	MAXUSERS },
61 	{ "option",	OPTIONS },
62 	{ "options",	OPTIONS },
63 	{ 0, 0 },
64 };
65 
66 
67 int kw_lookup(char *);
68 int octal(char *);
69 int hex(char *);
70 
71 %}
72 WORD	[A-Za-z_][-A-Za-z_]*
73 ID	[A-Za-z_][-A-Za-z_0-9]*
74 %START NONUM TOEOL
75 %%
76 <NONUM>{WORD}	{
77 			int i;
78 
79 			BEGIN 0;
80 			if ((i = kw_lookup(yytext)) == -1)
81 			{
82 				yylval.str = strdup(yytext);
83 				return ID;
84 			}
85 			return i;
86 		}
87 <INITIAL>{WORD}/[0-9]* {
88 			int i;
89 
90 			if ((i = kw_lookup(yytext)) == -1)
91 				REJECT;
92 			if (i == DEVICE)
93 				BEGIN NONUM;
94 			return i;
95 		}
96 <INITIAL>{ID}	{
97 			BEGIN 0;
98 			yylval.str = strdup(yytext);
99 			return ID;
100 		}
101 \\\"[^"]+\\\"	{
102 			BEGIN 0;
103 			yytext[yyleng-2] = '"';
104 			yytext[yyleng-1] = '\0';
105 			yylval.str = strdup(yytext + 1);
106 			return ID;
107 		}
108 \"[^"]+\"	{
109 			BEGIN 0;
110 			yytext[yyleng-1] = '\0';
111 			yylval.str = strdup(yytext + 1);
112 			return ID;
113 		}
114 <TOEOL>[^# \t\n]*	{
115 			BEGIN 0;
116 			yylval.str = strdup(yytext);
117 			return ID;
118 		}
119 0[0-7]*		{
120 			yylval.val = octal(yytext);
121 			return NUMBER;
122 		}
123 0x[0-9a-fA-F]+	{
124 			yylval.val = hex(yytext);
125 			return NUMBER;
126 		}
127 -?[1-9][0-9]*	{
128 			yylval.val = atoi(yytext);
129 			return NUMBER;
130 		}
131 "?"		{
132 			yylval.val = -1;
133 			return NUMBER;
134 		}
135 \n/[ \t]	{
136 			yyline++;
137 		}
138 \n		{
139 			yyline++;
140 			return SEMICOLON;
141 		}
142 #.*		{	/* Ignored (comment) */;	}
143 [ \t\f]*	{	/* Ignored (white space) */;	}
144 ";"		{	return SEMICOLON;		}
145 ","		{	return COMMA;			}
146 "="		{	BEGIN TOEOL; return EQUALS;	}
147 .		{	return yytext[0];		}
148 
149 %%
150 /*
151  * kw_lookup
152  *	Look up a string in the keyword table.  Returns a -1 if the
153  *	string is not a keyword otherwise it returns the keyword number
154  */
155 
156 int
157 kw_lookup(char *word)
158 {
159 	struct kt *kp;
160 
161 	for (kp = key_words; kp->kt_name != 0; kp++)
162 		if (eq(word, kp->kt_name))
163 			return kp->kt_val;
164 	return -1;
165 }
166 
167 /*
168  * Number conversion routines
169  */
170 
171 int
172 octal(char *str)
173 {
174 	int num;
175 
176 	(void) sscanf(str, "%o", &num);
177 	return num;
178 }
179 
180 int
181 hex(char *str)
182 {
183 	int num;
184 
185 	(void) sscanf(str+2, "%x", &num);
186 	return num;
187 }
188