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