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