xref: /freebsd/usr.sbin/config/lang.l (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
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  */
36 
37 #include <ctype.h>
38 #include <string.h>
39 #include "y.tab.h"
40 #include "config.h"
41 
42 #define tprintf if (do_trace) printf
43 
44 /*
45  * Key word table
46  */
47 
48 struct kt {
49 	char *kt_name;
50 	int kt_val;
51 } key_words[] = {
52 	{ "and",	AND },
53 	{ "args",	ARGS },
54 	{ "at",		AT },
55 #if MACHINE_I386
56 	{ "bio",	BIO },
57 	{ "bus",	BUS },
58 	{ "cam",	CAM},
59 	{ "conflicts",	CONFLICTS },
60 #endif
61 	{ "config",	CONFIG },
62 	{ "controller",	CONTROLLER },
63 	{ "cpu",	CPU },
64 	{ "csr",	CSR },
65 	{ "device",	DEVICE },
66 #if MACHINE_I386
67 	{ "disable",	DISABLE },
68 #endif
69 	{ "disk",	DISK },
70 	{ "drive",	DRIVE },
71 #if MACHINE_I386
72 	{ "drq",	DRQ },
73 #endif
74 	{ "dumps",	DUMPS },
75 	{ "flags",	FLAGS },
76 	{ "ident",	IDENT },
77 	{ "interleave",	INTERLEAVE },
78 #if MACHINE_I386
79 	{ "iomem",	IOMEM },
80 	{ "iosiz",	IOSIZ },
81 	{ "irq",	IRQ },
82 #endif
83 	{ "machine",	MACHINE },
84 	{ "major",	MAJOR },
85 	{ "makeoptions", MAKEOPTIONS },
86 	{ "master",	MASTER },
87 	{ "maxusers",	MAXUSERS },
88 	{ "minor",	MINOR },
89 #if MACHINE_I386
90 	{ "net",	NET },
91 #endif
92 	{ "nexus",	NEXUS },
93 	{ "on",		ON },
94 	{ "options",	OPTIONS },
95 #if MACHINE_I386
96 	{ "port",	PORT },
97 #endif
98 	{ "priority",	PRIORITY },
99 	{ "pseudo-device",PSEUDO_DEVICE },
100 	{ "root",	ROOT },
101 #if MACHINE_HP300 || MACHINE_LUNA68K
102 	{ "scode",	NEXUS },
103 #endif
104 	{ "sequential",	SEQUENTIAL },
105 	{ "size",	SIZE },
106 	{ "slave",	SLAVE },
107 	{ "swap",	SWAP },
108 	{ "tape",	DEVICE },
109 	{ "target",	TARGET },
110 #if MACHINE_I386
111 	{ "tty",	TTY },
112 #endif
113 	{ "trace",	TRACE },
114 	{ "unit",	UNIT },
115 	{ "vector",	VECTOR },
116 	{ 0, 0 },
117 };
118 
119 
120 int kw_lookup __P((char *));
121 int octal __P((char *));
122 int hex __P((char *));
123 
124 %}
125 WORD	[A-Za-z_][-A-Za-z_]*
126 %%
127 {WORD}		{
128 			int i;
129 
130 			if ((i = kw_lookup(yytext)) == -1)
131 			{
132 				yylval.str = strdup(yytext);
133 				tprintf("id(%s) ", yytext);
134 				return ID;
135 			}
136 			tprintf("(%s) ", yytext);
137 			return i;
138 		}
139 \\\"[^"]+\\\"	{
140 			yytext[strlen(yytext)-2] = '"';
141 			yytext[strlen(yytext)-1] = '\0';
142 			yylval.str = strdup(yytext + 1);
143 			return ID;
144 		}
145 \"[^"]+\"	{
146 			yytext[strlen(yytext)-1] = '\0';
147 			yylval.str = strdup(yytext + 1);
148 			return ID;
149 		}
150 0[0-7]*		{
151 			yylval.val = octal(yytext);
152 			tprintf("#O:%o ", yylval.val);
153 			return NUMBER;
154 		}
155 0x[0-9a-fA-F]+	{
156 			yylval.val = hex(yytext);
157 			tprintf("#X:%x ", yylval.val);
158 			return NUMBER;
159 		}
160 [1-9][0-9]*	{
161 			yylval.val = atoi(yytext);
162 			tprintf("#D:%d ", yylval.val);
163 			return NUMBER;
164 		}
165 [0-9]"."[0-9]*	{
166 			double atof();
167 			yylval.val = (int) (60 * atof(yytext) + 0.5);
168 			return FPNUMBER;
169 		}
170 "-"		{
171 			return MINUS;
172 		}
173 "?"		{
174 			yylval.val = -1;
175 			tprintf("? ");
176 			return NUMBER;
177 		}
178 \n/[ \t]	{
179 			yyline++;
180 			tprintf("\n... ");
181 		}
182 \n		{
183 			yyline++;
184 			tprintf("\n");
185 			return SEMICOLON;
186 		}
187 #.*		{	/* Ignored (comment) */;	}
188 [ \t\f]*	{	/* Ignored (white space) */;	}
189 ";"		{	return SEMICOLON;		}
190 ","		{	return COMMA;			}
191 "="		{	return EQUALS;			}
192 "@"		{	return AT;			}
193 .		{	return yytext[0];		}
194 
195 %%
196 /*
197  * kw_lookup
198  *	Look up a string in the keyword table.  Returns a -1 if the
199  *	string is not a keyword otherwise it returns the keyword number
200  */
201 
202 int
203 kw_lookup(word)
204 register char *word;
205 {
206 	register struct kt *kp;
207 
208 	for (kp = key_words; kp->kt_name != 0; kp++)
209 		if (eq(word, kp->kt_name))
210 			return kp->kt_val;
211 	return -1;
212 }
213 
214 /*
215  * Number conversion routines
216  */
217 
218 int
219 octal(str)
220 char *str;
221 {
222 	int num;
223 
224 	(void) sscanf(str, "%o", &num);
225 	return num;
226 }
227 
228 int
229 hex(str)
230 char *str;
231 {
232 	int num;
233 
234 	(void) sscanf(str+2, "%x", &num);
235 	return num;
236 }
237