xref: /freebsd/usr.sbin/config/lang.l (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
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  * $FreeBSD$
36  */
37 
38 #include <ctype.h>
39 #include <string.h>
40 #include "y.tab.h"
41 #include "config.h"
42 
43 #define YY_NO_UNPUT
44 
45 /*
46  * Key word table
47  */
48 
49 struct kt {
50 	const char *kt_name;
51 	int kt_val;
52 } key_words[] = {
53 	{ "config",	CONFIG },
54 	{ "cpu",	CPU },
55 	{ "device",	DEVICE },
56 	{ "hints",	HINTS },
57 	{ "ident",	IDENT },
58 	{ "machine",	ARCH }, /* MACHINE is defined in /sys/param.h */
59 	{ "makeoptions", MAKEOPTIONS },
60 	{ "maxusers",	MAXUSERS },
61 	{ "profile",	PROFILE },
62 	{ "option",	OPTIONS },
63 	{ "options",	OPTIONS },
64 	{ 0, 0 },
65 };
66 
67 
68 int kw_lookup(char *);
69 int octal(char *);
70 int hex(char *);
71 
72 %}
73 WORD	[A-Za-z_][-A-Za-z_]*
74 ID	[A-Za-z_][-A-Za-z_0-9]*
75 %START NONUM TOEOL
76 %%
77 <NONUM>{WORD}	{
78 			int i;
79 
80 			BEGIN 0;
81 			if ((i = kw_lookup(yytext)) == -1)
82 			{
83 				yylval.str = strdup(yytext);
84 				return ID;
85 			}
86 			return i;
87 		}
88 <INITIAL>{WORD}/[0-9]* {
89 			int i;
90 
91 			if ((i = kw_lookup(yytext)) == -1)
92 				REJECT;
93 			if (i == DEVICE)
94 				BEGIN NONUM;
95 			return i;
96 		}
97 <INITIAL>{ID}	{
98 			BEGIN 0;
99 			yylval.str = strdup(yytext);
100 			return ID;
101 		}
102 \\\"[^"]+\\\"	{
103 			BEGIN 0;
104 			yytext[yyleng-2] = '"';
105 			yytext[yyleng-1] = '\0';
106 			yylval.str = strdup(yytext + 1);
107 			return ID;
108 		}
109 \"[^"]+\"	{
110 			BEGIN 0;
111 			yytext[yyleng-1] = '\0';
112 			yylval.str = strdup(yytext + 1);
113 			return ID;
114 		}
115 <TOEOL>[^# \t\n]*	{
116 			BEGIN 0;
117 			yylval.str = strdup(yytext);
118 			return ID;
119 		}
120 0[0-7]*		{
121 			yylval.val = octal(yytext);
122 			return NUMBER;
123 		}
124 0x[0-9a-fA-F]+	{
125 			yylval.val = hex(yytext);
126 			return NUMBER;
127 		}
128 -?[1-9][0-9]*	{
129 			yylval.val = atoi(yytext);
130 			return NUMBER;
131 		}
132 "?"		{
133 			yylval.val = -1;
134 			return NUMBER;
135 		}
136 \n/[ \t]	{
137 			yyline++;
138 		}
139 \n		{
140 			yyline++;
141 			return SEMICOLON;
142 		}
143 #.*		{	/* Ignored (comment) */;	}
144 [ \t\f]*	{	/* Ignored (white space) */;	}
145 ";"		{	return SEMICOLON;		}
146 ","		{	return COMMA;			}
147 "="		{	BEGIN TOEOL; return EQUALS;	}
148 .		{	return yytext[0];		}
149 
150 %%
151 /*
152  * kw_lookup
153  *	Look up a string in the keyword table.  Returns a -1 if the
154  *	string is not a keyword otherwise it returns the keyword number
155  */
156 
157 int
158 kw_lookup(char *word)
159 {
160 	struct kt *kp;
161 
162 	for (kp = key_words; kp->kt_name != 0; kp++)
163 		if (eq(word, kp->kt_name))
164 			return kp->kt_val;
165 	return -1;
166 }
167 
168 /*
169  * Number conversion routines
170  */
171 
172 int
173 octal(char *str)
174 {
175 	int num;
176 
177 	(void) sscanf(str, "%o", &num);
178 	return num;
179 }
180 
181 int
182 hex(char *str)
183 {
184 	int num;
185 
186 	(void) sscanf(str+2, "%x", &num);
187 	return num;
188 }
189