xref: /freebsd/usr.sbin/config/lang.l (revision e627b39baccd1ec9129690167cf5e6d860509655)
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 	{ "auto",	AUTO },
56 #if MACHINE_I386
57 	{ "bio",	BIO },
58 	{ "bus",	BUS },
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 	{ "none",	NONE },
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 WORD	[A-Za-z_][-A-Za-z_]*
121 %%
122 {WORD}		{
123 			int i;
124 
125 			if ((i = kw_lookup(yytext)) == -1)
126 			{
127 				yylval.str = strdup(yytext);
128 				tprintf("id(%s) ", yytext);
129 				return ID;
130 			}
131 			tprintf("(%s) ", yytext);
132 			return i;
133 		}
134 \\\"[^"]+\\\"	{
135 			yytext[strlen(yytext)-2] = '"';
136 			yytext[strlen(yytext)-1] = '\0';
137 			yylval.str = strdup(yytext + 1);
138 			return ID;
139 		}
140 \"[^"]+\"	{
141 			yytext[strlen(yytext)-1] = '\0';
142 			yylval.str = strdup(yytext + 1);
143 			return ID;
144 		}
145 0[0-7]*		{
146 			yylval.val = octal(yytext);
147 			tprintf("#O:%o ", yylval.val);
148 			return NUMBER;
149 		}
150 0x[0-9a-fA-F]+	{
151 			yylval.val = hex(yytext);
152 			tprintf("#X:%x ", yylval.val);
153 			return NUMBER;
154 		}
155 [1-9][0-9]*	{
156 			yylval.val = atoi(yytext);
157 			tprintf("#D:%d ", yylval.val);
158 			return NUMBER;
159 		}
160 [0-9]"."[0-9]*	{
161 			double atof();
162 			yylval.val = (int) (60 * atof(yytext) + 0.5);
163 			return FPNUMBER;
164 		}
165 "-"		{
166 			return MINUS;
167 		}
168 "?"		{
169 			yylval.val = -1;
170 			tprintf("? ");
171 			return NUMBER;
172 		}
173 \n/[ \t]	{
174 			yyline++;
175 			tprintf("\n... ");
176 		}
177 \n		{
178 			yyline++;
179 			tprintf("\n");
180 			return SEMICOLON;
181 		}
182 #.*		{	/* Ignored (comment) */;	}
183 [ \t\f]*	{	/* Ignored (white space) */;	}
184 ";"		{	return SEMICOLON;		}
185 ","		{	return COMMA;			}
186 "="		{	return EQUALS;			}
187 "@"		{	return AT;			}
188 .		{	return yytext[0];		}
189 
190 %%
191 /*
192  * kw_lookup
193  *	Look up a string in the keyword table.  Returns a -1 if the
194  *	string is not a keyword otherwise it returns the keyword number
195  */
196 
197 kw_lookup(word)
198 register char *word;
199 {
200 	register struct kt *kp;
201 
202 	for (kp = key_words; kp->kt_name != 0; kp++)
203 		if (eq(word, kp->kt_name))
204 			return kp->kt_val;
205 	return -1;
206 }
207 
208 /*
209  * Number conversion routines
210  */
211 
212 octal(str)
213 char *str;
214 {
215 	int num;
216 
217 	(void) sscanf(str, "%o", &num);
218 	return num;
219 }
220 
221 hex(str)
222 char *str;
223 {
224 	int num;
225 
226 	(void) sscanf(str+2, "%x", &num);
227 	return num;
228 }
229