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